| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624 |
- /*
- * Copyright (C) 2015-2018 Alibaba Group Holding Limited
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <memory.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/prctl.h>
- #include <sys/time.h>
- #include <semaphore.h>
- #include <errno.h>
- #include <assert.h>
- #include <net/if.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/ioctl.h>
- #include <sys/reboot.h>
- #include <sys/time.h>
- #include <time.h>
- #include <signal.h>
- #include "infra_config.h"
- #include "infra_compat.h"
- #include "infra_defs.h"
- #include "wrappers_defs.h"
- #define PLATFORM_WAIT_INFINITE (~0)
- #ifdef DYNAMIC_REGISTER
- char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1ZETBPbycq";
- char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "L68wCVXYUaNg1Ey9";
- char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "example1";
- char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "";
- #else
- #ifdef DEVICE_MODEL_ENABLED
- char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1RIsMLz2BJ";
- char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "fSAF0hle6xL0oRWd";
- char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "example1";
- char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "RDXf67itLqZCwdMCRrw0N5FHbv5D7jrE";
- #else
- char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = "a1MZxOdcBnO";
- char _product_secret[IOTX_PRODUCT_SECRET_LEN + 1] = "h4I4dneEFp7EImTv";
- char _device_name[IOTX_DEVICE_NAME_LEN + 1] = "test_01";
- char _device_secret[IOTX_DEVICE_SECRET_LEN + 1] = "t9GmMf2jb3LgWfXBaZD2r3aJrfVWBv56";
- #endif
- #endif
- char _firmware_version[IOTX_FIRMWARE_VER_LEN] = "app-1.0.0-20180101.1000";
- void *HAL_Malloc(uint32_t size)
- {
- return malloc(size);
- }
- void *HAL_Realloc(void *ptr, uint32_t size)
- {
- return realloc(ptr, size);
- }
- void HAL_Free(void *ptr)
- {
- free(ptr);
- }
- uint64_t HAL_UptimeMs(void)
- {
- uint64_t time_ms;
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- time_ms = ((uint64_t)ts.tv_sec * (uint64_t)1000) + (ts.tv_nsec / 1000 / 1000);
- return time_ms;
- }
- void HAL_SleepMs(uint32_t ms)
- {
- usleep(1000 * ms);
- }
- void HAL_Srandom(uint32_t seed)
- {
- srandom(seed);
- }
- uint32_t HAL_Random(uint32_t region)
- {
- FILE *handle;
- ssize_t ret = 0;
- uint32_t output = 0;
- handle = fopen("/dev/urandom", "r");
- if (handle == NULL) {
- printf("open /dev/urandom failed\n");
- return 0;
- }
- ret = fread(&output, sizeof(uint32_t), 1, handle);
- if (ret != 1) {
- printf("fread error: %d\n", (int)ret);
- fclose(handle);
- return 0;
- }
- fclose(handle);
- return (region > 0) ? (output % region) : 0;
- }
- int HAL_Snprintf(char *str, const int len, const char *fmt, ...)
- {
- va_list args;
- int rc;
- va_start(args, fmt);
- rc = vsnprintf(str, len, fmt, args);
- va_end(args);
- return rc;
- }
- int HAL_Vsnprintf(char *str, const int len, const char *format, va_list ap)
- {
- return vsnprintf(str, len, format, ap);
- }
- void HAL_Printf(const char *fmt, ...)
- {
- va_list args;
- va_start(args, fmt);
- vprintf(fmt, args);
- va_end(args);
- fflush(stdout);
- }
- int HAL_GetPartnerID(char *pid_str)
- {
- memset(pid_str, 0x0, IOTX_PARTNER_ID_LEN);
- strcpy(pid_str, "c-sdk-2.3.0-pid");
- return strlen(pid_str);
- }
- int HAL_GetModuleID(char *mid_str)
- {
- memset(mid_str, 0x0, IOTX_MODULE_ID_LEN);
- strcpy(mid_str, "c-sdk-2.3.0-mid");
- return strlen(mid_str);
- }
- int HAL_SetProductKey(char *product_key)
- {
- int len = strlen(product_key);
- if (len > IOTX_PRODUCT_KEY_LEN) {
- return -1;
- }
- memset(_product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
- strncpy(_product_key, product_key, len);
- return len;
- }
- int HAL_SetDeviceName(char *device_name)
- {
- int len = strlen(device_name);
- if (len > IOTX_DEVICE_NAME_LEN) {
- return -1;
- }
- memset(_device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
- strncpy(_device_name, device_name, len);
- return len;
- }
- int HAL_SetProductSecret(char *product_secret)
- {
- int len = strlen(product_secret);
- if (len > IOTX_PRODUCT_SECRET_LEN) {
- return -1;
- }
- memset(_product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
- strncpy(_product_secret, product_secret, len);
- return len;
- }
- int HAL_SetDeviceSecret(char *device_secret)
- {
- int len = strlen(device_secret);
- if (len > IOTX_DEVICE_SECRET_LEN) {
- return -1;
- }
- memset(_device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
- strncpy(_device_secret, device_secret, len);
- return len;
- }
- int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1])
- {
- int len = strlen(_product_key);
- memset(product_key, 0x0, IOTX_PRODUCT_KEY_LEN + 1);
- strncpy(product_key, _product_key, len);
- return len;
- }
- int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1])
- {
- int len = strlen(_product_secret);
- memset(product_secret, 0x0, IOTX_PRODUCT_SECRET_LEN + 1);
- strncpy(product_secret, _product_secret, len);
- return len;
- }
- int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1])
- {
- int len = strlen(_device_name);
- memset(device_name, 0x0, IOTX_DEVICE_NAME_LEN + 1);
- strncpy(device_name, _device_name, len);
- return strlen(device_name);
- }
- int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
- {
- int len = strlen(_device_secret);
- memset(device_secret, 0x0, IOTX_DEVICE_SECRET_LEN + 1);
- strncpy(device_secret, _device_secret, len);
- return len;
- }
- void HAL_Reboot(void)
- {
- if (system("reboot")) {
- perror("HAL_Reboot failed");
- }
- }
- #define ROUTER_INFO_PATH "/proc/net/route"
- #define ROUTER_RECORD_SIZE 256
- char *_get_default_routing_ifname(char *ifname, int ifname_size)
- {
- FILE *fp = NULL;
- char line[ROUTER_RECORD_SIZE] = {0};
- char iface[IFNAMSIZ] = {0};
- char *result = NULL;
- unsigned int destination, gateway, flags, mask;
- unsigned int refCnt, use, metric, mtu, window, irtt;
- char *buff = NULL;
- fp = fopen(ROUTER_INFO_PATH, "r");
- if (fp == NULL) {
- perror("fopen");
- return result;
- }
- buff = fgets(line, sizeof(line), fp);
- if (buff == NULL) {
- perror("fgets");
- goto out;
- }
- while (fgets(line, sizeof(line), fp)) {
- if (11 !=
- sscanf(line, "%s %08x %08x %x %u %u %u %08x %u %u %u",
- iface, &destination, &gateway, &flags, &refCnt, &use,
- &metric, &mask, &mtu, &window, &irtt)) {
- perror("sscanf");
- continue;
- }
- /*default route */
- if ((destination == 0) && (mask == 0)) {
- strncpy(ifname, iface, ifname_size - 1);
- result = ifname;
- break;
- }
- }
- out:
- if (fp) {
- fclose(fp);
- }
- return result;
- }
- uint32_t HAL_Wifi_Get_IP(char ip_str[NETWORK_ADDR_LEN], const char *ifname)
- {
- struct ifreq ifreq;
- int sock = -1;
- char ifname_buff[IFNAMSIZ] = {0};
- if ((NULL == ifname || strlen(ifname) == 0) &&
- NULL == (ifname = _get_default_routing_ifname(ifname_buff, sizeof(ifname_buff)))) {
- perror("get default routeing ifname");
- return -1;
- }
- if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("socket");
- return -1;
- }
- ifreq.ifr_addr.sa_family = AF_INET;
- strncpy(ifreq.ifr_name, ifname, IFNAMSIZ - 1);
- if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
- close(sock);
- perror("ioctl");
- return -1;
- }
- close(sock);
- strncpy(ip_str,
- inet_ntoa(((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr),
- NETWORK_ADDR_LEN);
- return ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
- }
- int HAL_GetFirmwareVersion(char *version)
- {
- char *ver = "app-1.0.0-20180101.1000";
- int len = strlen(ver);
- memset(version, 0x0, IOTX_FIRMWARE_VER_LEN);
- strncpy(version, ver, IOTX_FIRMWARE_VER_LEN);
- version[len] = '\0';
- return strlen(version);
- }
- void *HAL_SemaphoreCreate(void)
- {
- sem_t *sem = (sem_t *)malloc(sizeof(sem_t));
- if (NULL == sem) {
- return NULL;
- }
- if (0 != sem_init(sem, 0, 0)) {
- free(sem);
- return NULL;
- }
- return sem;
- }
- void HAL_SemaphoreDestroy(void *sem)
- {
- sem_destroy((sem_t *)sem);
- free(sem);
- }
- void HAL_SemaphorePost(void *sem)
- {
- sem_post((sem_t *)sem);
- }
- int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
- {
- if (PLATFORM_WAIT_INFINITE == timeout_ms) {
- sem_wait(sem);
- return 0;
- } else {
- struct timespec ts;
- int s;
- /* Restart if interrupted by handler */
- do {
- if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
- return -1;
- }
- s = 0;
- ts.tv_nsec += (timeout_ms % 1000) * 1000000;
- if (ts.tv_nsec >= 1000000000) {
- ts.tv_nsec -= 1000000000;
- s = 1;
- }
- ts.tv_sec += timeout_ms / 1000 + s;
- } while (((s = sem_timedwait(sem, &ts)) != 0) && errno == EINTR);
- return (s == 0) ? 0 : -1;
- }
- }
- int HAL_ThreadCreate(
- void **thread_handle,
- void *(*work_routine)(void *),
- void *arg,
- hal_os_thread_param_t *hal_os_thread_param,
- int *stack_used)
- {
- int ret = -1;
- if (stack_used) {
- *stack_used = 0;
- }
- ret = pthread_create((pthread_t *)thread_handle, NULL, work_routine, arg);
- return ret;
- }
- void HAL_ThreadDetach(void *thread_handle)
- {
- pthread_detach((pthread_t)thread_handle);
- }
- void HAL_ThreadDelete(void *thread_handle)
- {
- if (NULL == thread_handle) {
- pthread_exit(0);
- } else {
- /*main thread delete child thread*/
- pthread_cancel((pthread_t)thread_handle);
- pthread_join((pthread_t)thread_handle, 0);
- }
- }
- static FILE *fp;
- #define otafilename "/tmp/alinkota.bin"
- void HAL_Firmware_Persistence_Start(void)
- {
- fp = fopen(otafilename, "w");
- return;
- }
- int HAL_Firmware_Persistence_Write(char *buffer, uint32_t length)
- {
- unsigned int written_len = 0;
- written_len = fwrite(buffer, 1, length, fp);
- if (written_len != length) {
- return -1;
- }
- return 0;
- }
- int HAL_Firmware_Persistence_Stop(void)
- {
- if (fp != NULL) {
- fclose(fp);
- }
- /* check file md5, and burning it to flash ... finally reboot system */
- return 0;
- }
- void *HAL_MutexCreate(void)
- {
- int err_num;
- pthread_mutex_t *mutex = (pthread_mutex_t *)HAL_Malloc(sizeof(pthread_mutex_t));
- if (NULL == mutex) {
- return NULL;
- }
- if (0 != (err_num = pthread_mutex_init(mutex, NULL))) {
- printf("create mutex failed\n");
- HAL_Free(mutex);
- return NULL;
- }
- return mutex;
- }
- void HAL_MutexDestroy(void *mutex)
- {
- int err_num;
- if (!mutex) {
- printf("mutex want to destroy is NULL!\n");
- return;
- }
- if (0 != (err_num = pthread_mutex_destroy((pthread_mutex_t *)mutex))) {
- printf("destroy mutex failed\n");
- }
- HAL_Free(mutex);
- }
- void HAL_MutexLock(void *mutex)
- {
- int err_num;
- if (0 != (err_num = pthread_mutex_lock((pthread_mutex_t *)mutex))) {
- printf("lock mutex failed: - '%s' (%d)\n", strerror(err_num), err_num);
- }
- }
- void HAL_MutexUnlock(void *mutex)
- {
- int err_num;
- if (0 != (err_num = pthread_mutex_unlock((pthread_mutex_t *)mutex))) {
- printf("unlock mutex failed - '%s' (%d)\n", strerror(err_num), err_num);
- }
- }
- void *HAL_Timer_Create(const char *name, void (*func)(void *), void *user_data)
- {
- timer_t *timer = NULL;
- struct sigevent ent;
- /* check parameter */
- if (func == NULL) {
- return NULL;
- }
- timer = (timer_t *)malloc(sizeof(time_t));
- if (timer == NULL) {
- return NULL;
- }
- /* Init */
- memset(&ent, 0x00, sizeof(struct sigevent));
- /* create a timer */
- ent.sigev_notify = SIGEV_THREAD;
- ent.sigev_notify_function = (void (*)(union sigval))func;
- ent.sigev_value.sival_ptr = user_data;
- printf("HAL_Timer_Create\n");
- if (timer_create(CLOCK_MONOTONIC, &ent, timer) != 0) {
- free(timer);
- return NULL;
- }
- return (void *)timer;
- }
- int HAL_Timer_Start(void *timer, int ms)
- {
- struct itimerspec ts;
- /* check parameter */
- if (timer == NULL) {
- return -1;
- }
- /* it_interval=0: timer run only once */
- ts.it_interval.tv_sec = 0;
- ts.it_interval.tv_nsec = 0;
- /* it_value=0: stop timer */
- ts.it_value.tv_sec = ms / 1000;
- ts.it_value.tv_nsec = (ms % 1000) * 1000000;
- return timer_settime(*(timer_t *)timer, 0, &ts, NULL);
- }
- int HAL_Timer_Stop(void *timer)
- {
- struct itimerspec ts;
- /* check parameter */
- if (timer == NULL) {
- return -1;
- }
- /* it_interval=0: timer run only once */
- ts.it_interval.tv_sec = 0;
- ts.it_interval.tv_nsec = 0;
- /* it_value=0: stop timer */
- ts.it_value.tv_sec = 0;
- ts.it_value.tv_nsec = 0;
- return timer_settime(*(timer_t *)timer, 0, &ts, NULL);
- }
- int HAL_Timer_Delete(void *timer)
- {
- int ret = 0;
- /* check parameter */
- if (timer == NULL) {
- return -1;
- }
- ret = timer_delete(*(timer_t *)timer);
- free(timer);
- return ret;
- }
- int HAL_GetNetifInfo(char *nif_str)
- {
- const char *net_info = "WiFi|03ACDEFF0032";
- memset(nif_str, 0x0, IOTX_NETWORK_IF_LEN);
- /* if the device have only WIFI, then list as follow, note that the len MUST NOT exceed NIF_STRLEN_MAX */
- strncpy(nif_str, net_info, IOTX_NETWORK_IF_LEN);
- /* if the device have ETH, WIFI, GSM connections, then list all of them as follow, note that the len MUST NOT exceed NIF_STRLEN_MAX */
- /* const char *multi_net_info = "ETH|0123456789abcde|WiFi|03ACDEFF0032|Cellular|imei_0123456789abcde|iccid_0123456789abcdef01234|imsi_0123456789abcde|msisdn_86123456789ab"); */
- /* strncpy(nif_str, multi_net_info, strlen(multi_net_info)); */
- return strlen(nif_str);
- }
|