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);
227 #if PLATFORM_IS_WINDOWS 231 return st.wMilliseconds;
236 gettimeofday(&tv,
NULL);
237 return tv.tv_usec / 1000;
247 #if PLATFORM_IS_WINDOWS 251 return st.wMilliseconds + 1000 * (st.wSecond + 60 * (st.wMinute + 60 * (st.wHour + 24 * st.wDayOfWeek)));
256 gettimeofday(&tv,
NULL);
257 return tv.tv_usec / 1000 + 1000 * tv.tv_sec;
266 #if PLATFORM_IS_WINDOWS 268 LARGE_INTEGER StartingTime;
269 LARGE_INTEGER Frequency;
271 QueryPerformanceCounter(&StartingTime);
272 QueryPerformanceFrequency(&Frequency);
274 StartingTime.QuadPart *= 1000000;
275 StartingTime.QuadPart /= Frequency.QuadPart;
277 return StartingTime.QuadPart;
282 gettimeofday(&tv,
NULL);
283 return tv.tv_usec + 1000000 * tv.tv_sec;
292 #if PLATFORM_IS_WINDOWS 294 LARGE_INTEGER StartingTime;
295 QueryPerformanceCounter(&StartingTime);
296 return StartingTime.QuadPart;
301 gettimeofday(&tv,
NULL);
302 return tv.tv_usec + 1000000 * tv.tv_sec;
311 #if PLATFORM_IS_WINDOWS 313 LARGE_INTEGER EndingTime, ElapsedTimeUs;
314 LARGE_INTEGER Frequency;
316 QueryPerformanceCounter(&EndingTime);
317 QueryPerformanceFrequency(&Frequency);
319 ElapsedTimeUs.QuadPart = EndingTime.QuadPart - start;
321 ElapsedTimeUs.QuadPart *= 1000000;
322 ElapsedTimeUs.QuadPart /= Frequency.QuadPart;
324 return ElapsedTimeUs.QuadPart;
329 gettimeofday(&tv,
NULL);
330 uint64_t stopTimeUs = tv.tv_usec + 1000000 * tv.tv_sec;
331 return stopTimeUs - start;
340 #if PLATFORM_IS_WINDOWS 342 LARGE_INTEGER StartingTime;
343 QueryPerformanceCounter(&StartingTime);
344 return StartingTime.QuadPart;
349 gettimeofday(&tv,
NULL);
350 return tv.tv_usec + 1000000 * tv.tv_sec;
359 #if PLATFORM_IS_WINDOWS 361 LARGE_INTEGER EndingTime, ElapsedTimeUs;
362 QueryPerformanceCounter(&EndingTime);
363 ElapsedTimeUs.QuadPart = EndingTime.QuadPart - start;
364 return ElapsedTimeUs.QuadPart;
369 gettimeofday(&tv,
NULL);
370 uint64_t stopTimeUs = tv.tv_usec + 1000000 * tv.tv_sec;
371 return stopTimeUs - start;
380 #if PLATFORM_IS_WINDOWS 382 return GetTickCount64();
384 #elif PLATFORM_IS_EVB_2 391 if (clock_gettime(CLOCK_MONOTONIC, &now))
395 return (uint64_t)(now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0);
406 printf(
"\rUpload: %d%% \r", (
int)(percent * 100.0
f));
421 printf(
"\rVerify: %d%% \r", (
int)(percent * 100.0
f));
437 float step_sinwave(
float *sig_gen,
float freqHz,
float amplitude,
float periodSec)
439 *sig_gen += freqHz * periodSec *
C_TWOPI_F;
447 return amplitude * sinf(*sig_gen);
456 fopen_s(&file, path, mode);
460 file = fopen(path, mode);
471 #if PLATFORM_IS_WINDOWS 473 static char _tempPath[MAX_PATH];
474 if (_tempPath[0] == 0)
476 GetTempPathA(MAX_PATH, _tempPath);
490 static const unsigned char s_hexLookupTable[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
491 return s_hexLookupTable;
496 return 9 * (hex >> 6) + (hex & 017);
501 #if PLATFORM_IS_EMBEDDED 505 #elif CPP11_IS_ENABLED 507 return new thread(
function, info);
509 #elif PLATFORM_IS_WINDOWS 511 return CreateThread(
NULL, 0, (LPTHREAD_START_ROUTINE)
function, info, 0,
NULL);
515 pthread_t* t = (pthread_t*)
MALLOC(
sizeof(pthread_t));
516 pthread_create(t,
NULL,
function, info);
530 #if PLATFORM_IS_EMBEDDED 534 #elif CPP11_IS_ENABLED 536 ((thread*)handle)->join();
537 delete (thread*)handle;
539 #elif PLATFORM_IS_WINDOWS 541 WaitForSingleObject(handle, 0);
546 pthread_join((pthread_t*)handle);
556 #if PLATFORM_IS_EMBEDDED 560 #elif CPP11_IS_ENABLED 564 #elif PLATFORM_IS_WINDOWS 566 CRITICAL_SECTION* c = (CRITICAL_SECTION*)
MALLOC(
sizeof(CRITICAL_SECTION));
567 InitializeCriticalSection(c);
572 pthread_mutex_t* m = (pthread_mutex_t*)
MALLOC(
sizeof(pthread_mutex_t));
573 pthread_mutex_init(m,
NULL);
583 #if PLATFORM_IS_EMBEDDED 587 #elif CPP11_IS_ENABLED 589 ((mutex*)handle)->lock();
591 #elif PLATFORM_IS_WINDOWS 593 EnterCriticalSection((CRITICAL_SECTION*)handle);
597 pthread_mutex_lock((pthread_mutex_t*)handle);
606 #if PLATFORM_IS_EMBEDDED 610 #elif CPP11_IS_ENABLED 612 ((mutex*)handle)->unlock();
614 #elif PLATFORM_IS_WINDOWS 616 LeaveCriticalSection((CRITICAL_SECTION*)handle);
620 pthread_mutex_unlock((pthread_mutex_t*)handle);
633 #if PLATFORM_IS_EMBEDDED 637 #elif CPP11_IS_ENABLED 639 delete (mutex*)handle;
641 #elif PLATFORM_IS_WINDOWS 643 DeleteCriticalSection((CRITICAL_SECTION*)handle);
648 pthread_mutex_destroy((pthread_mutex_t*)handle);
659 - 7 * (year + (month + 9) / 12) / 4
660 - 3 * ((year + (month - 9) / 7) / 100 + 1) / 4
669 uint32_t gpsDays = gpsWeek * 7 + (gpsSeconds / 86400);
677 j = mjd + 2400001 + 68569;
679 j = j - (146097 * c + 3) / 4;
680 y = 4000 * (j + 1) / 1461001;
681 j = j - 1461 * y / 4 + 31;
683 *day = j - 2447 * m / 80;
685 *month = m + 2 - (12 * j);
686 *year = 100 * (c - 49) + y + j;
689 void convertGpsToHMS(int32_t gpsSeconds, int32_t* hour, int32_t* minutes, int32_t* seconds)
692 gpsSeconds = gpsSeconds % 86400;
695 *hour = gpsSeconds / 3600;
696 *minutes = (gpsSeconds / 60) % 60;
697 *seconds = gpsSeconds % 60;
700 uint32_t
dateToWeekDay(uint32_t ul_year, uint32_t ul_month, uint32_t ul_day)
704 if (ul_month == 1 || ul_month == 2) {
709 ul_week = (ul_day + 2 * ul_month + 3 * (ul_month + 1) / 5 + ul_year +
710 ul_year / 4 - ul_year / 100 + ul_year / 400) % 7;
729 data.
val[0] = val[0];
730 data.
val[1] = val[1];
731 data.
val[2] = val[2];
739 data.
val1[0] = val1[0];
740 data.
val1[1] = val1[1];
741 data.
val1[2] = val1[2];
742 data.
val2[0] = val2[0];
743 data.
val2[1] = val2[1];
744 data.
val2[2] = val2[2];
752 data.
val[0] = val[0];
753 data.
val[1] = val[1];
754 data.
val[2] = val[2];
777 void cMutex::Unlock()
782 cMutexLocker::cMutexLocker(cMutex* mutex)
789 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 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)
uint64_t current_weekUs()
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)