Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
wiggly
Date:
Thu Aug 24 14:14:15 2017 +0100
Revision:
34:651c23af382c
Parent:
19:2e0811512ceb
Pass in network stack to platform initialisation
Remove NTP setup from azure platform code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 5 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 6:c55b013dfc2a 6 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 0:fa2de1b79154 7 #include "rtos.h"
Azure.IoT Build 0:fa2de1b79154 8
Azure.IoT Build 0:fa2de1b79154 9 DEFINE_ENUM_STRINGS(THREADAPI_RESULT, THREADAPI_RESULT_VALUES);
Azure.IoT Build 0:fa2de1b79154 10
Azure.IoT Build 0:fa2de1b79154 11 #define MAX_THREADS 4
Azure.IoT Build 0:fa2de1b79154 12 #define STACK_SIZE 0x4000
Azure.IoT Build 0:fa2de1b79154 13
Azure.IoT Build 0:fa2de1b79154 14 typedef struct _thread
Azure.IoT Build 0:fa2de1b79154 15 {
Azure.IoT Build 0:fa2de1b79154 16 Thread* thrd;
Azure.IoT Build 0:fa2de1b79154 17 osThreadId id;
Azure.IoT Build 0:fa2de1b79154 18 Queue<int, 1> result;
Azure.IoT Build 0:fa2de1b79154 19 } mbedThread;
Azure.IoT Build 0:fa2de1b79154 20 static mbedThread threads[MAX_THREADS] = { 0 };
Azure.IoT Build 0:fa2de1b79154 21
Azure.IoT Build 0:fa2de1b79154 22 typedef struct _create_param
Azure.IoT Build 0:fa2de1b79154 23 {
Azure.IoT Build 0:fa2de1b79154 24 THREAD_START_FUNC func;
Azure.IoT Build 0:fa2de1b79154 25 const void* arg;
Azure.IoT Build 0:fa2de1b79154 26 mbedThread *p_thread;
Azure.IoT Build 0:fa2de1b79154 27 } create_param;
Azure.IoT Build 0:fa2de1b79154 28
Azure.IoT Build 0:fa2de1b79154 29 static void thread_wrapper(const void* createParamArg)
Azure.IoT Build 0:fa2de1b79154 30 {
Azure.IoT Build 0:fa2de1b79154 31 const create_param* p = (const create_param*)createParamArg;
Azure.IoT Build 0:fa2de1b79154 32 p->p_thread->id = Thread::gettid();
Azure.IoT Build 0:fa2de1b79154 33 (*(p->func))((void*)p->arg);
Azure.IoT Build 0:fa2de1b79154 34 free((void*)p);
Azure.IoT Build 0:fa2de1b79154 35 }
Azure.IoT Build 0:fa2de1b79154 36
Azure.IoT Build 0:fa2de1b79154 37 THREADAPI_RESULT ThreadAPI_Create(THREAD_HANDLE* threadHandle, THREAD_START_FUNC func, void* arg)
Azure.IoT Build 0:fa2de1b79154 38 {
Azure.IoT Build 0:fa2de1b79154 39 THREADAPI_RESULT result;
Azure.IoT Build 0:fa2de1b79154 40 if ((threadHandle == NULL) ||
Azure.IoT Build 0:fa2de1b79154 41 (func == NULL))
Azure.IoT Build 0:fa2de1b79154 42 {
Azure.IoT Build 0:fa2de1b79154 43 result = THREADAPI_INVALID_ARG;
AzureIoTClient 1:9190c0f4d23a 44 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 45 }
Azure.IoT Build 0:fa2de1b79154 46 else
Azure.IoT Build 0:fa2de1b79154 47 {
Azure.IoT Build 0:fa2de1b79154 48 size_t slot;
Azure.IoT Build 0:fa2de1b79154 49 for (slot = 0; slot < MAX_THREADS; slot++)
Azure.IoT Build 0:fa2de1b79154 50 {
Azure.IoT Build 0:fa2de1b79154 51 if (threads[slot].id == NULL)
Azure.IoT Build 0:fa2de1b79154 52 break;
Azure.IoT Build 0:fa2de1b79154 53 }
Azure.IoT Build 0:fa2de1b79154 54
Azure.IoT Build 0:fa2de1b79154 55 if (slot < MAX_THREADS)
Azure.IoT Build 0:fa2de1b79154 56 {
Azure.IoT Build 0:fa2de1b79154 57 create_param* param = (create_param*)malloc(sizeof(create_param));
Azure.IoT Build 0:fa2de1b79154 58 if (param != NULL)
Azure.IoT Build 0:fa2de1b79154 59 {
Azure.IoT Build 0:fa2de1b79154 60 param->func = func;
Azure.IoT Build 0:fa2de1b79154 61 param->arg = arg;
Azure.IoT Build 0:fa2de1b79154 62 param->p_thread = threads + slot;
Azure.IoT Build 0:fa2de1b79154 63 threads[slot].thrd = new Thread(thread_wrapper, param, osPriorityNormal, STACK_SIZE);
Azure.IoT Build 0:fa2de1b79154 64 *threadHandle = (THREAD_HANDLE)(threads + slot);
Azure.IoT Build 0:fa2de1b79154 65 result = THREADAPI_OK;
Azure.IoT Build 0:fa2de1b79154 66 }
Azure.IoT Build 0:fa2de1b79154 67 else
Azure.IoT Build 0:fa2de1b79154 68 {
Azure.IoT Build 0:fa2de1b79154 69 result = THREADAPI_NO_MEMORY;
AzureIoTClient 1:9190c0f4d23a 70 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 71 }
Azure.IoT Build 0:fa2de1b79154 72 }
Azure.IoT Build 0:fa2de1b79154 73 else
Azure.IoT Build 0:fa2de1b79154 74 {
Azure.IoT Build 0:fa2de1b79154 75 result = THREADAPI_NO_MEMORY;
AzureIoTClient 1:9190c0f4d23a 76 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 77 }
Azure.IoT Build 0:fa2de1b79154 78 }
Azure.IoT Build 0:fa2de1b79154 79
Azure.IoT Build 0:fa2de1b79154 80 return result;
Azure.IoT Build 0:fa2de1b79154 81 }
Azure.IoT Build 0:fa2de1b79154 82
Azure.IoT Build 0:fa2de1b79154 83 THREADAPI_RESULT ThreadAPI_Join(THREAD_HANDLE thr, int *res)
Azure.IoT Build 0:fa2de1b79154 84 {
Azure.IoT Build 0:fa2de1b79154 85 THREADAPI_RESULT result = THREADAPI_OK;
Azure.IoT Build 0:fa2de1b79154 86 mbedThread* p = (mbedThread*)thr;
Azure.IoT Build 0:fa2de1b79154 87 if (p)
Azure.IoT Build 0:fa2de1b79154 88 {
Azure.IoT Build 0:fa2de1b79154 89 osEvent evt = p->result.get();
Azure.IoT Build 0:fa2de1b79154 90 if (evt.status == osEventMessage) {
Azure.IoT Build 0:fa2de1b79154 91 Thread* t = p->thrd;
Azure.IoT Build 0:fa2de1b79154 92 if (res)
Azure.IoT Build 0:fa2de1b79154 93 {
Azure.IoT Build 0:fa2de1b79154 94 *res = (int)evt.value.p;
Azure.IoT Build 0:fa2de1b79154 95 }
Azure.IoT Build 0:fa2de1b79154 96 (void)t->terminate();
Azure.IoT Build 0:fa2de1b79154 97 }
Azure.IoT Build 0:fa2de1b79154 98 else
Azure.IoT Build 0:fa2de1b79154 99 {
Azure.IoT Build 0:fa2de1b79154 100 result = THREADAPI_ERROR;
AzureIoTClient 1:9190c0f4d23a 101 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 102 }
Azure.IoT Build 0:fa2de1b79154 103 }
Azure.IoT Build 0:fa2de1b79154 104 else
Azure.IoT Build 0:fa2de1b79154 105 {
Azure.IoT Build 0:fa2de1b79154 106 result = THREADAPI_INVALID_ARG;
AzureIoTClient 1:9190c0f4d23a 107 LogError("(result = %s)", ENUM_TO_STRING(THREADAPI_RESULT, result));
Azure.IoT Build 0:fa2de1b79154 108 }
Azure.IoT Build 0:fa2de1b79154 109 return result;
Azure.IoT Build 0:fa2de1b79154 110 }
Azure.IoT Build 0:fa2de1b79154 111
Azure.IoT Build 0:fa2de1b79154 112 void ThreadAPI_Exit(int res)
Azure.IoT Build 0:fa2de1b79154 113 {
Azure.IoT Build 0:fa2de1b79154 114 mbedThread* p;
Azure.IoT Build 0:fa2de1b79154 115 for (p = threads; p < &threads[MAX_THREADS]; p++)
Azure.IoT Build 0:fa2de1b79154 116 {
Azure.IoT Build 0:fa2de1b79154 117 if (p->id == Thread::gettid())
Azure.IoT Build 0:fa2de1b79154 118 {
Azure.IoT Build 0:fa2de1b79154 119 p->result.put((int*)res);
Azure.IoT Build 0:fa2de1b79154 120 break;
Azure.IoT Build 0:fa2de1b79154 121 }
Azure.IoT Build 0:fa2de1b79154 122 }
Azure.IoT Build 0:fa2de1b79154 123 }
Azure.IoT Build 0:fa2de1b79154 124
Azure.IoT Build 0:fa2de1b79154 125 void ThreadAPI_Sleep(unsigned int millisec)
Azure.IoT Build 0:fa2de1b79154 126 {
Azure.IoT Build 0:fa2de1b79154 127 //
Azure.IoT Build 0:fa2de1b79154 128 // The timer on mbed seems to wrap around 65 seconds. Hmmm.
Azure.IoT Build 0:fa2de1b79154 129 // So we will do our waits in increments of 30 seconds.
Azure.IoT Build 0:fa2de1b79154 130 //
Azure.IoT Build 0:fa2de1b79154 131 const int thirtySeconds = 30000;
Azure.IoT Build 0:fa2de1b79154 132 int numberOfThirtySecondWaits = millisec / thirtySeconds;
Azure.IoT Build 0:fa2de1b79154 133 int remainderOfThirtySeconds = millisec % thirtySeconds;
Azure.IoT Build 0:fa2de1b79154 134 int i;
Azure.IoT Build 0:fa2de1b79154 135 for (i = 1; i <= numberOfThirtySecondWaits; i++)
Azure.IoT Build 0:fa2de1b79154 136 {
Azure.IoT Build 0:fa2de1b79154 137 Thread::wait(thirtySeconds);
Azure.IoT Build 0:fa2de1b79154 138 }
Azure.IoT Build 0:fa2de1b79154 139 Thread::wait(remainderOfThirtySeconds);
Azure.IoT Build 0:fa2de1b79154 140 }