1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 #ifndef __CELLULAR_PLATFORM_H__
28 #define __CELLULAR_PLATFORM_H__
29 
30 #include "FreeRTOS.h"
31 #include "queue.h"
32 #include "semphr.h"
33 #include "event_groups.h"
34 
35 #include <stdint.h>
36 #include <stdbool.h>
37 
38 /*-----------------------------------------------------------*/
39 
40 /**
41  * @brief Cellular library log configuration.
42  *
43  * Cellular library use CellularLogLevel macro for logging.
44  * The prototype of these logging function is similar with printf with return type ignored.
45  *
46  */
47 
48 #include "logging_levels.h"
49 #ifndef LIBRARY_LOG_NAME
50     #define LIBRARY_LOG_NAME     "CELLULAR"
51 #endif
52 #ifndef LIBRARY_LOG_LEVEL
53     #define LIBRARY_LOG_LEVEL    LOG_ERROR
54 #endif
55 
56 /* Map the SdkLog macro to the logging function to enable logging
57  * on Windows simulator. */
58 #ifndef SdkLog
59     #define SdkLog( message )    printf message
60 #endif
61 #include "logging_stack.h"
62 
63 
64 
65 /*-----------------------------------------------------------*/
66 
67 /**
68  * @brief Cellular library platform thread API and configuration.
69  *
70  * Cellular library create a detached thread by this API.
71  * The threadRoutine should be called with pArgument in the created thread.
72  *
73  * PLATFORM_THREAD_DEFAULT_STACK_SIZE and PLATFORM_THREAD_DEFAULT_PRIORITY defines
74  * the platform related stack size and priority.
75  */
76 
77 bool Platform_CreateDetachedThread( void ( * threadRoutine )( void * ),
78                                     void * pArgument,
79                                     int32_t priority,
80                                     size_t stackSize );
81 
82 #define PLATFORM_THREAD_DEFAULT_STACK_SIZE    ( 2048U )
83 #define PLATFORM_THREAD_DEFAULT_PRIORITY      ( tskIDLE_PRIORITY + 5U )
84 
85 /*-----------------------------------------------------------*/
86 
87 /**
88  * @brief Cellular library platform mutex APIs.
89  *
90  * Cellular library use platform mutex to protect resource.
91  *
92  * The IotMutex_ functions can be referenced as function prototype for
93  * PlatfromMutex_ prefix function in the following link.
94  * https://docs.aws.amazon.com/freertos/latest/lib-ref/c-sdk/platform/platform_threads_functions.html
95  *
96  */
97 
98 typedef struct PlatformMutex
99 {
100     StaticSemaphore_t xMutex; /**< FreeRTOS mutex. */
101     BaseType_t recursive;     /**< Type; used for indicating if this is reentrant or normal. */
102 } PlatformMutex_t;
103 
104 bool PlatformMutex_Create( PlatformMutex_t * pNewMutex,
105                            bool recursive );
106 void PlatformMutex_Destroy( PlatformMutex_t * pMutex );
107 void PlatformMutex_Lock( PlatformMutex_t * pMutex );
108 bool PlatformMutex_TryLock( PlatformMutex_t * pMutex );
109 void PlatformMutex_Unlock( PlatformMutex_t * pMutex );
110 
111 /*-----------------------------------------------------------*/
112 
113 /**
114  * @brief Cellular library platform memory allocation APIs.
115  *
116  * Cellular library use platform memory allocation APIs to allocate memory dynamically.
117  * The FreeRTOS memory management document can be referenced for these APIs.
118  * https://www.freertos.org/a00111.html
119  *
120  */
121 
122 #define Platform_Malloc    pvPortMalloc
123 #define Platform_Free      vPortFree
124 
125 /*-----------------------------------------------------------*/
126 
127 /**
128  * @brief Cellular library platform event group APIs.
129  *
130  * Cellular library use platform event group for process synchronization.
131  *
132  * The EventGroup functions in the following link can be referenced as function prototype.
133  * https://www.freertos.org/event-groups-API.html
134  *
135  */
136 
137 #define PlatformEventGroupHandle_t           EventGroupHandle_t
138 #define PlatformEventGroup_Delete            vEventGroupDelete
139 #define PlatformEventGroup_ClearBits         xEventGroupClearBits
140 #define PlatformEventGroup_Create            xEventGroupCreate
141 #define PlatformEventGroup_GetBits           xEventGroupGetBits
142 #define PlatformEventGroup_SetBits           xEventGroupSetBits
143 #define PlatformEventGroup_SetBitsFromISR    xEventGroupSetBitsFromISR
144 #define PlatformEventGroup_WaitBits          xEventGroupWaitBits
145 #define PlatformEventGroup_EventBits         EventBits_t
146 #define PlatformTickType                     TickType_t
147 
148 /*-----------------------------------------------------------*/
149 
150 /**
151  * @brief Cellular library platform delay function.
152  *
153  * Cellular library use platform delay function for waiting events.
154  *
155  * The delay functions in the following link can be referenced as function prototype.
156  * https://www.freertos.org/a00127.html
157  *
158  */
159 #define Platform_Delay( delayMs )    vTaskDelay( pdMS_TO_TICKS( delayMs ) )
160 
161 #endif /* __CELLULAR_PLATFORM_H__ */
162