22 #if PLATFORM_IS_EMBEDDED 24 #include "../EVB-2/IS_EVB-2/src/drivers/d_time.h" 26 #elif CPP11_IS_ENABLED 31 #elif PLATFORM_IS_WINDOWS 36 #elif PLATFORM_IS_LINUX 41 #error "Unsupported platform" 47 static const string s_base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
53 string base64Encode(
const unsigned char* bytes_to_encode,
unsigned int in_len)
58 unsigned char char_array_3[3];
59 unsigned char char_array_4[4];
63 char_array_3[i++] = *(bytes_to_encode++);
66 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
67 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
68 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
69 char_array_4[3] = char_array_3[2] & 0x3f;
70 for (i = 0; (i < 4); i++)
80 for (j = i; j < 3; j++)
82 char_array_3[j] =
'\0';
85 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
86 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
87 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
88 char_array_4[3] = char_array_3[2] & 0x3f;
90 for (j = 0; (j < i + 1); j++)
107 int in_len = (int)encoded_string.size();
111 unsigned char char_array_4[4], char_array_3[3];
114 while (in_len-- && (encoded_string[in_] !=
'=') &&
is_base64(encoded_string[in_]))
116 char_array_4[i++] = encoded_string[in_];
120 for (i = 0; i < 4; i++)
122 char_array_4[i] = (
unsigned char)
s_base64_chars.find(char_array_4[i]);
124 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
125 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
126 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
127 for (i = 0; (i < 3); i++)
129 ret += char_array_3[i];
137 for (j = i; j < 4; j++)
141 for (j = 0; j < 4; j++)
143 char_array_4[j] = (
unsigned char)
s_base64_chars.find(char_array_4[j]);
145 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
146 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
147 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
148 for (j = 0; (j < i - 1); j++)
150 ret += char_array_3[j];
157 size_t splitString(
const string& s,
const string& delimiter, vector<string>& result)
164 pos2 = s.find(delimiter, pos);
165 if (pos2 == string::npos)
167 result.push_back(s.substr(pos));
172 result.push_back(s.substr(pos, pos2 - pos));
173 pos = pos2 + delimiter.length();
176 return result.size();
183 #if PLATFORM_IS_WINDOWS 185 void usleep(__int64 usec)
190 ft.QuadPart = -(10 * usec);
192 timer = CreateWaitableTimer(
NULL,
true,
NULL);
193 SetWaitableTimer(timer, &ft, 0,
NULL,
NULL, 0);
194 WaitForSingleObject(timer, INFINITE);
201 #include <sys/time.h> 208 #if PLATFORM_IS_WINDOWS 217 gettimeofday(&tv,
NULL);
228 #if PLATFORM_IS_WINDOWS 233 return st.wMilliseconds + 1000 * (st.wSecond + 60 * (st.wMinute + 60 * (st.wHour + 24 * st.wDayOfWeek)));
239 gettimeofday(&tv,
NULL);
240 return tv.tv_usec / 1000 + 1000 * tv.tv_sec;
250 #if PLATFORM_IS_WINDOWS 253 LARGE_INTEGER StartingTime;
254 LARGE_INTEGER Frequency;
256 QueryPerformanceCounter(&StartingTime);
257 QueryPerformanceFrequency(&Frequency);
259 StartingTime.QuadPart *= 1000000;
260 StartingTime.QuadPart /= Frequency.QuadPart;
262 return StartingTime.QuadPart;
268 gettimeofday(&tv,
NULL);
269 return tv.tv_usec + 1000000 * tv.tv_sec;
278 #if PLATFORM_IS_WINDOWS 280 LARGE_INTEGER StartingTime;
281 QueryPerformanceCounter(&StartingTime);
282 return StartingTime.QuadPart;
288 gettimeofday(&tv,
NULL);
289 return tv.tv_usec + 1000000 * tv.tv_sec;
298 #if PLATFORM_IS_WINDOWS 300 LARGE_INTEGER EndingTime, ElapsedTimeUs;
301 LARGE_INTEGER Frequency;
303 QueryPerformanceCounter(&EndingTime);
304 QueryPerformanceFrequency(&Frequency);
306 ElapsedTimeUs.QuadPart = EndingTime.QuadPart - start;
308 ElapsedTimeUs.QuadPart *= 1000000;
309 ElapsedTimeUs.QuadPart /= Frequency.QuadPart;
311 return ElapsedTimeUs.QuadPart;
316 gettimeofday(&tv,
NULL);
317 uint64_t stopTimeUs = tv.tv_usec + 1000000 * tv.tv_sec;
318 return stopTimeUs - start;
327 #if PLATFORM_IS_WINDOWS 329 LARGE_INTEGER StartingTime;
330 QueryPerformanceCounter(&StartingTime);
331 return StartingTime.QuadPart;
336 gettimeofday(&tv,
NULL);
337 return tv.tv_usec + 1000000 * tv.tv_sec;
346 #if PLATFORM_IS_WINDOWS 348 LARGE_INTEGER EndingTime, ElapsedTimeUs;
349 QueryPerformanceCounter(&EndingTime);
350 ElapsedTimeUs.QuadPart = EndingTime.QuadPart - start;
351 return ElapsedTimeUs.QuadPart;
356 gettimeofday(&tv,
NULL);
357 uint64_t stopTimeUs = tv.tv_usec + 1000000 * tv.tv_sec;
358 return stopTimeUs - start;
367 #if PLATFORM_IS_WINDOWS 369 return GetTickCount64();
371 #elif PLATFORM_IS_EVB_2 378 if (clock_gettime(CLOCK_MONOTONIC, &now))
382 return (uint64_t)(now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0);
393 printf(
"\rUpload: %d%% \r", (
int)(percent * 100.0
f));
408 printf(
"\rVerify: %d%% \r", (
int)(percent * 100.0
f));
424 float step_sinwave(
float *sig_gen,
float freqHz,
float amplitude,
float periodSec)
426 *sig_gen += freqHz * periodSec *
C_TWOPI_F;
434 return amplitude * sinf(*sig_gen);
443 fopen_s(&file, path, mode);
447 file = fopen(path, mode);
458 #if PLATFORM_IS_WINDOWS 460 static char _tempPath[MAX_PATH];
461 if (_tempPath[0] == 0)
463 GetTempPathA(MAX_PATH, _tempPath);
477 static const unsigned char s_hexLookupTable[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
478 return s_hexLookupTable;
483 return 9 * (hex >> 6) + (hex & 017);
488 #if PLATFORM_IS_EMBEDDED 492 #elif CPP11_IS_ENABLED 494 return new thread(
function, info);
496 #elif PLATFORM_IS_WINDOWS 498 return CreateThread(
NULL, 0, (LPTHREAD_START_ROUTINE)
function, info, 0,
NULL);
502 pthread_t* t = (pthread_t*)
MALLOC(
sizeof(pthread_t));
503 pthread_create(t,
NULL,
function, info);
517 #if PLATFORM_IS_EMBEDDED 521 #elif CPP11_IS_ENABLED 523 ((thread*)handle)->join();
524 delete (thread*)handle;
526 #elif PLATFORM_IS_WINDOWS 528 WaitForSingleObject(handle, 0);
533 pthread_join((pthread_t*)handle);
543 #if PLATFORM_IS_EMBEDDED 547 #elif CPP11_IS_ENABLED 551 #elif PLATFORM_IS_WINDOWS 553 CRITICAL_SECTION* c = (CRITICAL_SECTION*)
MALLOC(
sizeof(CRITICAL_SECTION));
554 InitializeCriticalSection(c);
559 pthread_mutex_t* m = (pthread_mutex_t*)
MALLOC(
sizeof(pthread_mutex_t));
560 pthread_mutex_init(m,
NULL);
570 #if PLATFORM_IS_EMBEDDED 574 #elif CPP11_IS_ENABLED 576 ((mutex*)handle)->lock();
578 #elif PLATFORM_IS_WINDOWS 580 EnterCriticalSection((CRITICAL_SECTION*)handle);
584 pthread_mutex_lock((pthread_mutex_t*)handle);
593 #if PLATFORM_IS_EMBEDDED 597 #elif CPP11_IS_ENABLED 599 ((mutex*)handle)->unlock();
601 #elif PLATFORM_IS_WINDOWS 603 LeaveCriticalSection((CRITICAL_SECTION*)handle);
607 pthread_mutex_unlock((pthread_mutex_t*)handle);
620 #if PLATFORM_IS_EMBEDDED 624 #elif CPP11_IS_ENABLED 626 delete (mutex*)handle;
628 #elif PLATFORM_IS_WINDOWS 630 DeleteCriticalSection((CRITICAL_SECTION*)handle);
635 pthread_mutex_destroy((pthread_mutex_t*)handle);
646 - 7 * (year + (month + 9) / 12) / 4
647 - 3 * ((year + (month - 9) / 7) / 100 + 1) / 4
656 uint32_t gpsDays = gpsWeek * 7 + (gpsSeconds / 86400);
664 j = mjd + 2400001 + 68569;
666 j = j - (146097 * c + 3) / 4;
667 y = 4000 * (j + 1) / 1461001;
668 j = j - 1461 * y / 4 + 31;
670 *day = j - 2447 * m / 80;
672 *month = m + 2 - (12 * j);
673 *year = 100 * (c - 49) + y + j;
676 void convertGpsToHMS(int32_t gpsSeconds, int32_t* hour, int32_t* minutes, int32_t* seconds)
679 gpsSeconds = gpsSeconds % 86400;
682 *hour = gpsSeconds / 3600;
683 *minutes = (gpsSeconds / 60) % 60;
684 *seconds = gpsSeconds % 60;
687 uint32_t
dateToWeekDay(uint32_t ul_year, uint32_t ul_month, uint32_t ul_day)
691 if (ul_month == 1 || ul_month == 2) {
696 ul_week = (ul_day + 2 * ul_month + 3 * (ul_month + 1) / 5 + ul_year +
697 ul_year / 4 - ul_year / 100 + ul_year / 400) % 7;
716 data.
val[0] = val[0];
717 data.
val[1] = val[1];
718 data.
val[2] = val[2];
726 data.
val1[0] = val1[0];
727 data.
val1[1] = val1[1];
728 data.
val1[2] = val1[2];
729 data.
val2[0] = val2[0];
730 data.
val2[1] = val2[1];
731 data.
val2[2] = val2[2];
739 data.
val[0] = val[0];
740 data.
val[1] = val[1];
741 data.
val[2] = val[2];
764 void cMutex::Unlock()
769 cMutexLocker::cMutexLocker(cMutex* mutex)
776 cMutexLocker::~cMutexLocker()
void threadJoinAndFree(void *handle)
float step_sinwave(float *sig_gen, float freqHz, float amplitude, float periodSec)
Initialize signal generator.
void * threadCreateAndStart(void(*function)(void *info), void *info)
uint32_t dateToWeekDay(uint32_t ul_year, uint32_t ul_month, uint32_t ul_day)
size_t splitString(const string &s, const string &delimiter, vector< string > &result)
int bootloadUploadProgress(const void *port, float percent)
uint64_t timerRawEnd(uint64_t start)
int bootloadVerifyProgress(const void *port, float percent)
uint64_t current_timeUs()
uint64_t getTickCount(void)
int32_t convertDateToMjd(int32_t year, int32_t month, int32_t day)
gen_1axis_sensor_t gen1AxisSensorData(double time, const float val)
void mutexUnlock(void *handle)
void bootloadStatusInfo(const void *port, const char *str)
const unsigned char * getHexLookupTable()
volatile uint64_t time_ticks(void)
string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len)
FILE * openFile(const char *path, const char *mode)
uint8_t getHexValue(unsigned char hex)
static const string s_base64_chars
void convertMjdToDate(int32_t mjd, int32_t *year, int32_t *month, int32_t *day)
USBInterfaceDescriptor data
gen_3axis_sensor_t gen3AxisSensorData(double time, const float val[3])
uint64_t timerUsEnd(uint64_t start)
static bool is_base64(unsigned char c)
void mutexFree(void *handle)
int32_t convertGpsToMjd(int32_t gpsWeek, int32_t gpsSeconds)
gen_dual_3axis_sensor_t genDual3AxisSensorData(double time, const float val1[3], const float val2[3])
void convertGpsToHMS(int32_t gpsSeconds, int32_t *hour, int32_t *minutes, int32_t *seconds)
string base64Decode(const string &encoded_string)
gen_3axis_sensord_t gen3AxisSensorDataD(double time, const double val[3])
void mutexLock(void *handle)