EVE 1.0
rtos.h
Go to the documentation of this file.
1 #ifndef EVE_RTOS_H_INCLUDED
2 #define EVE_RTOS_H_INCLUDED
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2016, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * \brief Header file for the rtos emulation layer.
36  *
37  * \author DT, Jetro AS
38  */ /******************************************************************/
39 
40 /**
41  * \addtogroup rtos RTOS emulation
42  * \{
43  * RTOS emulation module provides an API layer which can be used to simplify
44  * porting of existing RTOS applications to EVE platform.
45  * The API mostly resembles FreeRTOS API, but implementation has a lot of deviations.
46  * - All tasks have same priority.
47  * - Preemptive multitasking is not supported.
48  * - Stacks are allocated dynamically from a pool.
49  * - Implementation is thread-safe against soft irqs (EVE work, mwork), but not against
50  * hard irqs (the only exception is xTimerPendFunctionCallFromISR()).\n
51  * Only xTimerPendFunctionCallFromISR() can be called from a hard irq context.
52  * Note that it can be used as a trampolino.
53  * - Timers are implemented as EVE mworks, not as a RTOS task. Timers therefore can preempt tasks.
54  * - There is no idle task in system.
55  * - Task scheduler is initially started.
56  * - Mutexes do not support priority inheritence and work as ordinary semaphores.
57  * - Object handles are typed pointers, not void pointers.
58  * - Most, if not all ...FromISR() functions alias their task-level analogs.
59  * - taskENTER_CRITICAL() disables soft irqs to ensure a thread is not interrupted by a timer.
60  * - taskDISABLE_INTERRUPTS() disables soft and hard irqs.
61  * .
62  * The layer is suboptimal and is not intended for creating either energy effective
63  * or real-time applications. At the same time it uses much less RAM per task due to
64  * stack sharing.
65  */
66 
67 /**
68  * \addtogroup common Common FreeRTOS-compatible types
69  * @{
70  */
71 #define pdPASS 1 /**< pdPASS return code */
72 #define pdFALSE false /**< pdFALSE return code */
73 #define pdTRUE true /**< pdTRUE return code */
74 typedef int32_t BaseType_t; /**< BaseType_t type */
75 typedef uint32_t UBaseType_t; /**< UBaseType_t type */
76 typedef uint32_t TickType_t; /**< TickType_t type */
77 
78 /** @} */
79 
80 #include <sys/types.h>
81 #include <net/uip.h>
82 
83 /* extern */ struct timeval;
84 
85 /**
86  * \addtogroup sock Berkeley sockets emulation
87  * @{
88  */
89 #define AF_INET 2 /**< INET address family */
90 #define SOCK_STREAM 1 /**< Stream socket type */
91 #define SOCK_DGRAM 2 /**< Datagram socket type */
92 #define IPPROTO_TCP 6 /**< TCP protocol */
93 #define IPPROTO_UDP 17 /**< UDP protocol */
94 #define SOL_SOCKET 1 /**< setsockopt() socket options */
95 #define SO_RCVTIMEO 20 /**< recv() timeout */
96 #define SO_SNDTIMEO 21 /**< send() timeout */
97 #define TCP_NODELAY 1 /**< Disable Nagle's algorithm */
98 #define FIONBIO 0x5421 /**< Async. socket IOCTL */
99 
100 
101 /** @} */
102 
103 /**
104  * \addtogroup task Task emulation
105  * @{
106  */
107 
108 struct Task_t;
109 typedef struct Task_t *TaskHandle_t; /**< TaskHandle_t type */
110 typedef void (*TaskFunction_t)(void * pvParameters); /**< TaskFunction_t type */
111 
112 /**
113  * portYIELD_FROM_ISR stub.
114  * Implementation does not support context switch from an ISR.
115  */
116 #define portYIELD_FROM_ISR(woken) do { } while (0)
117 
118 /**
119  * xTaskCreate emulation.
120  * http://www.freertos.org/a00125.html
121  * \note
122  * - usStackDepth is ignored, stack is dynamically allocated from a pool
123  * - uxPriority is ignored, all tasks run at same priority level
124  */
126  TaskFunction_t pvTaskCode,
127  const char * const pcName,
128  unsigned short usStackDepth,
129  void *pvParameters,
130  UBaseType_t uxPriority,
131  TaskHandle_t *pvCreatedTask);
132 
133 /**
134  * vTaskDelete emulation.
135  * http://www.freertos.org/a00126.html
136  */
137 void vTaskDelete(TaskHandle_t xTask);
138 
139 /**
140  * vTaskDelay emulation.
141  * http://www.freertos.org/a00127.html
142  */
143 void vTaskDelay(const TickType_t xTicksToDelay);
144 
145 /**
146  * vTaskDelayUntil emulation.
147  * http://www.freertos.org/vtaskdelayuntil.html
148  */
149 void vTaskDelayUntil(
150  TickType_t *pxPreviousWakeTime,
151  const TickType_t xTimeIncrement);
152 
153 /**
154  * taskYIELD emulation.
155  * http://www.freertos.org/a00020.html#taskYIELD
156  */
157 void taskYIELD(void);
158 
159 /**
160  * taskENTER_CRITICAL emulation.
161  * http://www.freertos.org/a00020.html#taskENTER_CRITICAL
162  */
163 void taskENTER_CRITICAL(void);
164 
165 /**
166  * taskEXIT_CRITICAL emulation.
167  * http://www.freertos.org/a00020.html#taskEXIT_CRITICAL
168  */
169 void taskEXIT_CRITICAL(void);
170 
171 /**
172  * taskDISABLE_INTERRUPTS emulation.
173  * http://www.freertos.org/a00020.html#taskDISABLE_INTERRUPTS
174  */
175 void taskDISABLE_INTERRUPTS(void);
176 
177 /**
178  * taskENABLE_INTERRUPTS emulation.
179  * http://www.freertos.org/a00020.html#taskENABLE_INTERRUPTS
180  */
181 void taskENABLE_INTERRUPTS(void);
182 
183 /**
184  * uxTaskPriorityGet stub.
185  * http://www.freertos.org/a00128.html
186  * \note
187  * - Implementation does not support task priorities.
188  */
190 {
191  return 1;
192 }
193 
194 /**
195  * vTaskPrioritySet stub.
196  * http://www.freertos.org/a00129.html
197  * \note
198  * - Implementation does not support task priorities.
199  */
200 static inline void vTaskPrioritySet(
201  TaskHandle_t xTask,
202  UBaseType_t uxNewPriority)
203 {
204 }
205 
206 /**
207  * vTaskSuspend emulation.
208  * http://www.freertos.org/a00130.html
209  */
210 void vTaskSuspend(TaskHandle_t xTaskToSuspend);
211 
212 /**
213  * vTaskResume emulation.
214  * http://www.freertos.org/a00131.html
215  */
216 void vTaskResume(TaskHandle_t xTaskToResume);
217 
218 /**
219  * xTaskResumeFromISR emulation.
220  * http://www.freertos.org/taskresumefromisr.html
221  * \note
222  * - Implementation never requests context switch from an ISR
223  * - This function can not be called from a hard irq context.
224  */
225 static inline BaseType_t xTaskResumeFromISR(TaskHandle_t xTaskToResume)
226 {
227  vTaskResume(xTaskToResume);
228  return pdFALSE;
229 }
230 
231 /**
232  * vTaskStartScheduler stub.
233  * http://www.freertos.org/a00132.html
234  * \note
235  * - Implementation just exits the current thread
236  */
237 void vTaskStartScheduler(void);
238 
239 /**
240  * Cancel any of blocking calls the task is currently waiting in.
241  * The blocking call exits with errno set to EINTR
242  */
243 void vTaskCancelBlockingCall(TaskHandle_t xTaskToWakeup);
244 
245 /** @} */
246 
247 /**
248  * \addtogroup semaphores Semaphore and Mutex emulation
249  * @{
250  */
251 
252 /* Dummy */ struct DummySemaphoreQueue_t;
253 typedef struct DummySemaphoreQueue_t *SemaphoreHandle_t; /**< SemaphoreHandle_t type */
254 typedef struct DummySemaphoreQueue_t *QueueHandle_t; /**< QueueHandle_t type */
255 
256 /**
257  * \def vSemaphoreCreateBinary
258  * vSemaphoreCreateBinary emulation
259  *
260  * http://www.freertos.org/a00121.html
261  */
262 #define vSemaphoreCreateBinary(xSemaphore) \
263  do { \
264  (xSemaphore) = xSemaphoreCreateBinary(); \
265  xSemaphoreGive(xSemaphore); \
266  } while (0)
267 
268 /**
269  * Creates a senaphore / mutex.
270  * \internal
271  */
272 SemaphoreHandle_t xSemaphoreCreateImpl(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, bool xRecursive);
273 
274 /**
275  * xSemaphoreCreateBinary emulation.
276  * http://www.freertos.org/xSemaphoreCreateBinary.html
277  */
279 {
280  return xSemaphoreCreateImpl(1, 0, false);
281 }
282 
283 /**
284  * xSemaphoreCreateCounting emulation.
285  * http://www.freertos.org/CreateCounting.html
286  */
287 static inline SemaphoreHandle_t xSemaphoreCreateCounting(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount)
288 {
289  return xSemaphoreCreateImpl(uxMaxCount, uxInitialCount, false);
290 }
291 
292 /**
293  * xSemaphoreCreateMutex emulation.
294  * http://www.freertos.org/CreateMutex.html
295  */
297 {
298  return xSemaphoreCreateImpl(1, 1, false);
299 }
300 
301 /**
302  * xSemaphoreCreateRecursiveMutex emulation.
303  * http://www.freertos.org/xSemaphoreCreateRecursiveMutex.html
304  */
306 {
307  return xSemaphoreCreateImpl(0xFFFFFFFF, 0, true);
308 }
309 
310 /**
311  * vSemaphoreDelete emulation.
312  * http://www.freertos.org/a00113.html#vSemaphoreDelete
313  */
314 void vSemaphoreDelete(SemaphoreHandle_t xSemaphore);
315 
316 /**
317  * xSemaphoreGetMutexHolder emulation.
318  * http://www.freertos.org/xSemaphoreGetMutexHolder.html
319  */
321 
322 /**
323  * xSemaphoreGive emulation.
324  * http://www.freertos.org/a00123.html
325  */
327 
328 /**
329  * xSemaphoreGiveFromISR emulation.
330  * http://www.freertos.org/a00124.html
331  * \note
332  * - This function can not be called from a hard irq context
333  */
335  SemaphoreHandle_t xSemaphore,
336  BaseType_t *const pxHigherPriorityTaskWoken)
337 {
338  return xSemaphoreGive(xSemaphore);
339 }
340 
341 /**
342  * xSemaphoreGiveRecursive emulation.
343  * http://www.freertos.org/xSemaphoreGiveRecursive.html
344  */
346 {
347  return xSemaphoreGive(xSemaphore);
348 }
349 
350 /**
351  * xSemaphoreTake emulation.
352  * http://www.freertos.org/a00123.html
353  */
354 BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait);
355 
356 /**
357  * xSemaphoreTakeFromISR emulation.
358  * http://www.freertos.org/xSemaphoreTakeFromISR.html
359  * \note
360  * - This function can not be called from a hard irq context
361  */
363  SemaphoreHandle_t xSemaphore,
364  BaseType_t *const pxHigherPriorityTaskWoken)
365 {
366  return xSemaphoreTake(xSemaphore, 0);
367 }
368 
369 /**
370  * xSemaphoreTakeRecursive emulation.
371  * http://www.freertos.org/a00122.html
372  */
373 static inline BaseType_t xSemaphoreTakeRecursive(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait)
374 {
375  return xSemaphoreTake(xSemaphore, xTicksToWait);
376 }
377 
378 /** @} */
379 
380 /**
381  * \addtogroup timeremu Timer emulation
382  * @{
383  */
384 
385 struct Timer_t;
386 typedef struct Timer_t* TimerHandle_t; /**< TimerHandle_t type */
387 typedef void (*TimerCallbackFunction_t)(TimerHandle_t pxTimer); /**< TimerCallbackFunction_t type */
388 typedef void (*PendedFunction_t)(void *pvParameter1, uint32_t ulParameter2); /**< PendedFunction_t type */
389 
390 /**
391  * xTimerCreate emulation.
392  * http://www.freertos.org/FreeRTOS-timers-xTimerCreate.html
393  */
395  const char *const pcTimerName,
396  const TickType_t xTimerPeriod,
397  const UBaseType_t uxAutoReload,
398  void *const pvTimerID,
399  TimerCallbackFunction_t pxCallbackFunction);
400 
401 /**
402  * xTimerIsTimerActive emulation.
403  * http://www.freertos.org/FreeRTOS-timers-xTimerIsTimerActive.html
404  */
406 
407 /**
408  * xTimerStart emulation.
409  * http://www.freertos.org/FreeRTOS-timers-xTimerStart.html
410  * \note
411  * - xBlockTime is ignored. Timer always starts.
412  */
414  TimerHandle_t xTimer,
415  TickType_t xBlockTime);
416 
417 /**
418  * xTimerStop emulation.
419  * http://www.freertos.org/FreeRTOS-timers-xTimerStop.html
420  * \note
421  * - xBlockTime is ignored. Timer always stops.
422  * - The function is synchronous, no callbacks will be called after it exits.
423  */
425  TimerHandle_t xTimer,
426  TickType_t xBlockTime);
427 
428 /**
429  * xTimerChangePeriod emulation.
430  * http://www.freertos.org/FreeRTOS-timers-xTimerChangePeriod.html
431  * \note
432  * - xBlockTime is ignored. Timer always stops.
433  */
435  TimerHandle_t xTimer,
436  TickType_t xNewPeriod,
437  TickType_t xBlockTime);
438 
439 /**
440  * xTimerDelete emulation.
441  * http://www.freertos.org/FreeRTOS-timers-xTimerDelete.html
442  * \note
443  * - xBlockTime is ignored. Timer always stops.
444  */
446  TimerHandle_t xTimer,
447  TickType_t xBlockTime);
448 
449 /**
450  * xTimerReset emulation.
451  * http://www.freertos.org/FreeRTOS-timers-xTimerReset.html
452  * \note
453  * - xBlockTime is ignored. Timer always stops.
454  */
455 static inline BaseType_t xTimerReset(
456  TimerHandle_t xTimer,
457  TickType_t xBlockTime)
458 {
459  return xTimerStart(xTimer, xBlockTime);
460 }
461 
462 /**
463  * xTimerStartFromISR emulation.
464  * http://www.freertos.org/FreeRTOS-timers-xTimerStartFromISR.html
465  * \note
466  * - Implementation never requests context switch from an ISR
467  * - This function can not be called from a hard irq context
468  */
470  TimerHandle_t xTimer,
471  BaseType_t *pxHigherPriorityTaskWoken)
472 {
473  return xTimerStart(xTimer, 0);
474 }
475 
476 /**
477  * xTimerStopFromISR emulation.
478  * http://www.freertos.org/FreeRTOS-timers-xTimerStopFromISR.html
479  * \note
480  * - Implementation never requests context switch from an ISR
481  * - The function is synchronous, no callbacks will be called after it exits.
482  * - This function can not be called from a hard irq context
483  */
485  TimerHandle_t xTimer,
486  BaseType_t *pxHigherPriorityTaskWoken)
487 {
488  return xTimerStop(xTimer, 0);
489 }
490 
491 /**
492  * xTimerChangePeriodFromISR emulation.
493  * http://www.freertos.org/FreeRTOS-timers-xTimerChangePeriodFromISR.html
494  * \note
495  * - Implementation never requests context switch from an ISR
496  * - This function can not be called from a hard irq context
497  */
499  TimerHandle_t xTimer,
500  TickType_t xNewPeriod,
501  BaseType_t *pxHigherPriorityTaskWoken)
502 {
503  return xTimerChangePeriod(xTimer, xNewPeriod, 0);
504 }
505 
506 /**
507  * xTimerResetFromISR emulation.
508  * http://www.freertos.org/FreeRTOS-timers-xTimerResetFromISR.html
509  * \note
510  * - Implementation never requests context switch from an ISR
511  * - This function can not be called from a hard irq context
512  */
514  TimerHandle_t xTimer,
515  BaseType_t *pxHigherPriorityTaskWoken)
516 {
517  return xTimerReset(xTimer, 0);
518 }
519 
520 /**
521  * pvTimerGetTimerID emulation.
522  * http://www.freertos.org/FreeRTOS-timers-pvTimerGetTimerID.html
523  */
524 void *pvTimerGetTimerID(TimerHandle_t xTimer);
525 
526 /**
527  * vTimerSetTimerID emulation.
528  * http://www.freertos.org/FreeRTOS-timers-vTimerSetTimerID.html
529  */
530 void vTimerSetTimerID(TimerHandle_t xTimer, void *pvNewID);
531 
532 /**
533  * pcTimerGetTimerName emulation.
534  * http://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html
535  */
536 const char *pcTimerGetTimerName(TimerHandle_t xTimer);
537 
538 
539 /**
540  * xTimerGetTimerDaemonTaskHandle stub.
541  * http://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html#xTimerGetTimerDaemonTaskHandle
542  * \note
543  * - Implementation does not use a timer daemon
544  */
545 #define xTimerGetTimerDaemonTaskHandle() (!!! "Not implemented" !!!)
546 
547 /**
548  * xTimerPendFunctionCall emulation.
549  * http://www.freertos.org/xTimerPendFunctionCall.html
550  */
552  PendedFunction_t xFunctionToPend,
553  void *pvParameter1,
554  uint32_t ulParameter2,
555  TickType_t xTicksToWait);
556 
557 /**
558  * xTimerPendFunctionCallFromISR emulation.
559  * http://www.freertos.org/xTimerPendFunctionCallFromISR.html
560  * \note
561  * - This function can be called from a hard irq context
562  * - The xFunctionToPend will be executed in a soft irq context
563  */
565  PendedFunction_t xFunctionToPend,
566  void *pvParameter1,
567  uint32_t ulParameter2,
568  BaseType_t *pxHigherPriorityTaskWoken)
569 {
570  return xTimerPendFunctionCall(xFunctionToPend, pvParameter1, ulParameter2, 0);
571 }
572 
573 /** @} */
574 
575 /**
576  * \addtogroup event Event group emulation
577  * @{
578  */
579 
580 struct EventGroup_t;
581 typedef uint32_t EventBits_t; /**< EventBits_t type */
582 typedef struct EventGroup_t *EventGroupHandle_t; /**< EventGroupHandle_t type */
583 
584 /**
585  * xEventGroupCreate emulation.
586  * http://www.freertos.org/xEventGroupCreate.html
587  */
589 
590 /**
591  * xEventGroupWaitBits emulation.
592  * http://www.freertos.org/xEventGroupWaitBits.html
593  */
595  const EventGroupHandle_t xEventGroup,
596  const EventBits_t uxBitsToWaitFor,
597  const BaseType_t xClearOnExit,
598  const BaseType_t xWaitForAllBits,
599  TickType_t xTicksToWait);
600 
601 /**
602  * xEventGroupSetBits emulation.
603  * http://www.freertos.org/xEventGroupSetBits.html
604  */
606  EventGroupHandle_t xEventGroup,
607  const EventBits_t uxBitsToSet);
608 
609 /**
610  * xEventGroupSetBitsFromISR emulation.
611  * http://www.freertos.org/xEventGroupSetBitsFromISR.html
612  */
614  EventGroupHandle_t xEventGroup,
615  const EventBits_t uxBitsToSet,
616  BaseType_t *pxHigherPriorityTaskWoken)
617 {
618  return xEventGroupSetBits(xEventGroup, uxBitsToSet);
619 }
620 
621 /**
622  * xEventGroupClearBits emulation.
623  * http://www.freertos.org/xEventGroupClearBits.html
624  */
626  EventGroupHandle_t xEventGroup,
627  const EventBits_t uxBitsToClear);
628 
629 /**
630  * xEventGroupClearBitsFromISR emulation.
631  * http://www.freertos.org/xEventGroupClearBitsFromISR.html
632  */
634  EventGroupHandle_t xEventGroup,
635  const EventBits_t uxBitsToClear)
636 {
637  return xEventGroupClearBits(xEventGroup, uxBitsToClear);
638 }
639 
640 /**
641  * xEventGroupGetBits emulation.
642  * http://www.freertos.org/xEventGroupGetBits.html
643  */
645 
646 /**
647  * xEventGroupGetBitsFromISR
648  * http://www.freertos.org/xEventGroupGetBitsFromISR.html
649  */
651 {
652  return xEventGroupGetBits(xEventGroup);
653 }
654 
655 /**
656  * xEventGroupSync emulation
657  * http://www.freertos.org/xEventGroupSync.html
658  */
660  EventGroupHandle_t xEventGroup,
661  const EventBits_t uxBitsToSet,
662  const EventBits_t uxBitsToWaitFor,
663  TickType_t xTicksToWait);
664 
665 /**
666  * vEventGroupDelete emulation.
667  * http://www.freertos.org/vEventGroupDelete.html
668  */
669 void vEventGroupDelete(EventGroupHandle_t xEventGroup);
670 
671 /** @} */
672 
673 /**
674  * \addtogroup sock
675  * @{
676  */
677 
678 /*extern*/ struct md_heap_t;
679 extern const struct md_heap_t RtosSocketRxHeap;
680 extern const struct md_heap_t RtosSocketTxHeap;
681 
682 enum rtos_socket_swap_direction_t
683 {
684  RTOS_SOCKET_SWAP_IN,
685  RTOS_SOCKET_SWAP_OUT,
686 };
687 
688 enum rtos_socket_swap_target_t
689 {
690  RTOS_SOCKET_SWAP_RX,
691  RTOS_SOCKET_SWAP_TX,
692 };
693 
694 struct rtos_socket_swap_params_t
695 {
696  enum rtos_socket_swap_direction_t Dir;
697  enum rtos_socket_swap_target_t Target;
698  void *Data;
699  uint32_t Pos;
700  uint32_t Size;
701 };
702 
703 extern bool RtosSocketSwapCallback(const struct rtos_socket_swap_params_t *Params);
704 
705 bool RtosSocketRxHeapSwapOut(const struct md_heap_t *RxHeap, uint32_t Pos, const uint8_t **DataPtr, uint32_t Size);
706 bool RtosSocketRxHeapSwapIn(const struct md_heap_t *RxHeap, uint8_t **DataPtr, uint32_t Pos, uint32_t Size);
707 bool RtosSocketTxHeapSwapOut(const struct md_heap_t *TxHeap, uint32_t Pos, const uint8_t **DataPtr, uint32_t Size);
708 bool RtosSocketTxHeapSwapIn(const struct md_heap_t *TxHeap, uint8_t **DataPtr, uint32_t Pos, uint32_t Size);
709 
710 #define RTOS_SOCKET_HEAP_NUM_ENTRIES(NumSockets) \
711  ((NumSockets) + 4)
712 
713 #define RTOS_SOCKET_HEAP_RAM_BUFFER_SIZE(NumSockets) \
714  ((NumSockets) * (1500 + 4) + 4)
715 
716 #define RTOS_SOCKET_HEAP_TINY_RAM_BUFFER_SIZE(NumSockets) \
717  ((NumSockets) * 4 + 1500 + 4)
718 
719 #define RTOS_SOCKET_HEAP_EXT_RAM_SIZE(NumSockets) \
720  ((NumSockets) * 1500)
721 
722 #define RTOS_SOCKET_HEAP_IMPL(NumSockets, Direction) \
723  static struct md_descriptor_t RtosSocket ## Direction ## HeapEntries[RTOS_SOCKET_HEAP_NUM_ENTRIES(NumSockets)]; \
724  static uint8_t RtosSocket ## Direction ## HeapRamBuffer[RTOS_SOCKET_HEAP_RAM_BUFFER_SIZE(NumSockets)]; \
725  static struct md_state_t RtosSocket ## Direction ## HeapState; \
726  const struct md_heap_t RtosSocket ## Direction ## Heap; \
727  static void RtosSocket ## Direction ## HeapCtor(void) __attribute__((constructor)); \
728  static void RtosSocket ## Direction ## HeapCtor(void) \
729  { \
730  MemInit(&RtosSocket ## Direction ## Heap); \
731  } \
732  const struct md_heap_t RtosSocket ## Direction ## Heap = \
733  { \
734  .Entries = RtosSocket ## Direction ## HeapEntries, \
735  .NumEntries = RTOS_SOCKET_HEAP_NUM_ENTRIES(NumSockets), \
736  .RamBuffer = RtosSocket ## Direction ## HeapRamBuffer, \
737  .RamBufferSize = RTOS_SOCKET_HEAP_RAM_BUFFER_SIZE(NumSockets), \
738  .State = &RtosSocket ## Direction ## HeapState, \
739  .ExtRamSize = 0, \
740  .SwapOut = NULL, \
741  .SwapIn = NULL, \
742  }
743 #define RTOS_SOCKET_HEAP(NumSockets) \
744  RTOS_SOCKET_HEAP_IMPL(NumSockets, Rx); \
745  RTOS_SOCKET_HEAP_IMPL(NumSockets, Tx)
746 
747 #define RTOS_SOCKET_TINY_HEAP_IMPL(NumSockets, Direction) \
748  static struct md_descriptor_t RtosSocket ## Direction ## HeapEntries[RTOS_SOCKET_HEAP_NUM_ENTRIES(NumSockets)]; \
749  static uint8_t RtosSocket ## Direction ## HeapRamBuffer[RTOS_SOCKET_HEAP_TINY_RAM_BUFFER_SIZE(NumSockets)]; \
750  static struct md_state_t RtosSocket ## Direction ## HeapState; \
751  const struct md_heap_t RtosSocket ## Direction ## Heap; \
752  static void RtosSocket ## Direction ## HeapCtor(void) __attribute__((constructor)); \
753  static void RtosSocket ## Direction ## HeapCtor(void) \
754  { \
755  MemInit(&RtosSocket ## Direction ## Heap); \
756  } \
757  const struct md_heap_t RtosSocket ## Direction ## Heap = \
758  { \
759  .Entries = RtosSocket ## Direction ## HeapEntries, \
760  .NumEntries = RTOS_SOCKET_HEAP_NUM_ENTRIES(NumSockets), \
761  .RamBuffer = RtosSocket ## Direction ## HeapRamBuffer, \
762  .RamBufferSize = RTOS_SOCKET_HEAP_TINY_RAM_BUFFER_SIZE(NumSockets), \
763  .State = &RtosSocket ## Direction ## HeapState, \
764  .ExtRamSize = RTOS_SOCKET_HEAP_EXT_RAM_SIZE(NumSockets), \
765  .SwapOut = RtosSocket ## Direction ## HeapSwapOut, \
766  .SwapIn = RtosSocket ## Direction ## HeapSwapIn, \
767  }
768 #define RTOS_SOCKET_TINY_HEAP(NumSockets) \
769  RTOS_SOCKET_TINY_HEAP_IMPL(NumSockets, Rx); \
770  RTOS_SOCKET_TINY_HEAP_IMPL(NumSockets, Tx)
771 
772 
773 typedef uint16_t sa_family_t; /**< Socket address family type */
774 typedef uint32_t socklen_t; /**< Socket address length type */
775 
776 /**
777  * Abstract socket address.
778  */
779 struct sockaddr
780 {
781  sa_family_t sa_family; /**< Socket address family */
782  char sa_data[14]; /**< Abstract data */
783 };
784 
785 /**
786  * IP address.
787  */
788 struct in_addr
789 {
790  uint32_t s_addr; /**< Address */
791 };
792 
793 /**
794  * Internet socket address.
795  */
797 {
798  sa_family_t sin_family; /**< Socket address family (AF_INET) */
799  uint16_t sin_port; /**< Socket port, big endian */
800  struct in_addr sin_addr; /**< IP address */
801  unsigned char pad[sizeof(struct sockaddr) - sizeof(sa_family_t) - sizeof(uint16_t) - sizeof(struct in_addr)];
802 };
803 
804 /**
805  * Host to network order
806  *
807  * \param hostshort Host-order short value
808  * \return Network-order short value
809  */
810 #define HTONS(hostshort) UIP_HTONS(hostshort)
811 
812 /**
813  * Host to network order
814  *
815  * \param hostshort Host-order short value
816  * \return Network-order short value
817  */
818 static inline uint16_t htons(uint16_t hostshort)
819 {
820  return UIP_HTONS(hostshort);
821 }
822 
823 /**
824  * Creates an endpoint for communication and returns a descriptor for the socket.
825  *
826  * \param domain The protocol family of the created socket.
827  * \param type SOCK_STREAM or SOCK_DGRAM
828  * \param protocol The actual transport protocol to use. 0 or IPPROTO_xxx
829  * \return -1 if an error occurred. Otherwise, it returns an integer representing the newly assigned descriptor.
830  */
831 int socket(int domain, int type, int protocol);
832 
833 /**
834  * Assigns a socket to an address.
835  *
836  * \param sockfd A descriptor representing the socket to perform the bind on.
837  * \param addr A pointer to a sockaddr structure representing the address to bind to.
838  * \param addrlen The size of the sockaddr structure, pointed by addr.
839  * \return 0 on success or -1 if an error occurs.
840  */
841 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
842 
843 /**
844  * Prepares a socket for incoming connections.
845  *
846  * \param sockfd A socket descriptor.
847  * \param backlog The number of pending connections that can be queued up at any one time (ignored by the current implementation).
848  * \return 0 on success or -1 if an error occurs.
849  */
850 int listen(int sockfd, int backlog);
851 
852 /**
853  * Creates a new socket for an incoming connection.
854  *
855  * \param sockfd A descriptor representing the listening socket.
856  * \param addr A pointer to a sockaddr structure to receive the client's address information.
857  * \param addrlen A pointer to a variable, containing size of the sockaddr structure. Updated by the call.
858  */
859 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
860 
861 /**
862  * Establishes a communication link towards a remote host.
863  *
864  * \param sockfd A local socket descriptor
865  * \param addr A pointer to a sockaddr structure representing the address of the remote host.
866  * \param addrlen The size of the sockaddr structure, pointed by addr.
867  * \return 0 on success or -1 if an error occurs.
868  */
869 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
870 
871 /**
872  * Closes a socket desctiptor.
873  *
874  * \param sockfd A socket descriptor
875  * \return 0 on success or -1 if an error occurs.
876  */
877 int closesocket(int sockfd);
878 
879 /**
880  * Receives data from a socket
881  *
882  * \param sockfd A socket descriptor.
883  * \param buf The buffer to be filled by data
884  * \param len Length of the buffer.
885  * \param flags Ignored in the current implementation
886  * \return Amount of bytes received or -1 if an error occurs.
887  */
888 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
889 
890 /**
891  * Waits for a socket becomes available for data reading
892  *
893  * If this function returns 0 for a socket, then a consequent recv() call
894  * will not block and will not return EWOULDBLOCK.
895  *
896  * \param sockfd A socket descriptor.
897  * \param timeout Timeout in system ticks.
898  * \return 0 if the socket becomes available, or -1 if not.
899  */
900 int poll_recv(int sockfd, uint32_t timeout);
901 
902 /**
903  * Sends data into a socket
904  *
905  * \param sockfd A socket descriptor.
906  * \param buf The buffer to be sent
907  * \param len Length of the buffer.
908  * \param flags Ignored in the current implementation
909  * \return Amount of bytes sent or -1 if an error occurs.
910  */
911 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
912 
913 /**
914  * Waits for a socket becomes available for data sending
915  *
916  * If thiss function returns 0 for a socket, then a consequent send() call
917  * will not block and will not return EWOULDBLOCK.
918  *
919  * \param sockfd A socket descriptor.
920  * \param timeout Timeout in system ticks.
921  * \return 0 if the socket becomes available, or -1 if not.
922  */
923 int poll_send(int sockfd, uint32_t timeout);
924 
925 /**
926  * Synchronous I/O multiplexing
927  *
928  * \param nfds The highest-numbered socket descriptor plus 1.
929  * \param readfds set of sockets, watched for read, updated on return.
930  * \param writefds set of sockets, watched for write, updated on return.
931  * \param exceptfds set of sockets, watched for exception, updated on return.
932  * \param timeout timeout. Infinite if NULL.
933  * \return the total number of bits set in the readfds, writefds, exceptfds.
934  */
935 int select(int nfds, fd_set *readfds, fd_set *writefds,
936  fd_set *exceptfds, struct timeval *timeout);
937 
938 /**
939  * Controls socket options (IOCTL ones).
940  *
941  * \param sockfd A socket descriptor.
942  * \param request IOCTL request (so far FIONBIO is the only supported)
943  * \param opt The option value
944  * \return 0
945  *
946  * \code
947 int opt = 1;
948 ioctlsocket(fd, FIONBIO, &opt);
949  * \endcode
950  */
951 int ioctlsocket(int sockfd, int request, const void *opt);
952 
953 /**
954  * Controls socket options (setsockopt ones).
955  *
956  * \param sockfd A socket descriptor.
957  * \param level Option level (SOL_SOCKET, IPPROTO_TCP)
958  * \param request The option name (SO_RCVTIMEO, SO_SNDTIMEO, TCP_NODELAY)
959  * \param opt The option value
960  * \param length Length of the option
961  * \return 0
962  *
963  * \code
964 struct timeval tv;
965 tv.tv_sec = 30;
966 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
967 setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
968 
969 int flag = 1;
970 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
971  * \endcode
972  */
973 int setsockopt(int sockfd, int level, int request, const void *opt, socklen_t length);
974 
975 /** \} */ /* sock */
976 
977 /** \} */ /* rtos */
978 
979 #endif
static BaseType_t xTimerStartFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:469
int closesocket(int sockfd)
static SemaphoreHandle_t xSemaphoreCreateBinary(void)
Definition: rtos.h:278
ssize_t send(int sockfd, const void *buf, size_t len, int flags)
const char * pcTimerGetTimerName(TimerHandle_t xTimer)
static UBaseType_t uxTaskPriorityGet(TaskHandle_t xTask)
Definition: rtos.h:189
static BaseType_t xSemaphoreGiveRecursive(SemaphoreHandle_t xSemaphore)
Definition: rtos.h:345
EventBits_t xEventGroupClearBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear)
struct Task_t * TaskHandle_t
Definition: rtos.h:109
uint32_t EventBits_t
Definition: rtos.h:580
uint16_t sa_family_t
Definition: rtos.h:773
void vTaskDelay(const TickType_t xTicksToDelay)
int socket(int domain, int type, int protocol)
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore)
struct EventGroup_t * EventGroupHandle_t
Definition: rtos.h:582
static BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xBlockTime)
Definition: rtos.h:455
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
ssize_t recv(int sockfd, void *buf, size_t len, int flags)
static EventBits_t xEventGroupGetBitsFromISR(EventGroupHandle_t xEventGroup)
Definition: rtos.h:650
void taskENABLE_INTERRUPTS(void)
sa_family_t sa_family
Definition: rtos.h:781
void vSemaphoreDelete(SemaphoreHandle_t xSemaphore)
void * pvTimerGetTimerID(TimerHandle_t xTimer)
int setsockopt(int sockfd, int level, int request, const void *opt, socklen_t length)
struct Timer_t * TimerHandle_t
Definition: rtos.h:386
void(* TimerCallbackFunction_t)(TimerHandle_t pxTimer)
Definition: rtos.h:387
sa_family_t sin_family
Definition: rtos.h:798
EventGroupHandle_t xEventGroupCreate(void)
SemaphoreHandle_t xSemaphoreCreateImpl(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, bool xRecursive)
uint32_t UBaseType_t
Definition: rtos.h:75
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
void vTaskDelayUntil(TickType_t *pxPreviousWakeTime, const TickType_t xTimeIncrement)
void taskYIELD(void)
void taskDISABLE_INTERRUPTS(void)
void vEventGroupDelete(EventGroupHandle_t xEventGroup)
struct dlist_t * backlog
Definition: usbnet.h:139
int listen(int sockfd, int backlog)
#define pdFALSE
Definition: rtos.h:72
struct DummySemaphoreQueue_t * QueueHandle_t
Definition: rtos.h:254
static uint16_t htons(uint16_t hostshort)
Definition: rtos.h:818
void vTaskSuspend(TaskHandle_t xTaskToSuspend)
static SemaphoreHandle_t xSemaphoreCreateMutex(void)
Definition: rtos.h:296
void taskEXIT_CRITICAL(void)
BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, const char *const pcName, unsigned short usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pvCreatedTask)
Definition: rtos.h:779
void vTimerSetTimerID(TimerHandle_t xTimer, void *pvNewID)
static BaseType_t xSemaphoreTakeRecursive(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait)
Definition: rtos.h:373
static BaseType_t xTimerResetFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:513
Definition: rtos.h:788
uint16_t sin_port
Definition: rtos.h:799
int poll_send(int sockfd, uint32_t timeout)
BaseType_t xTimerStop(TimerHandle_t xTimer, TickType_t xBlockTime)
void(* PendedFunction_t)(void *pvParameter1, uint32_t ulParameter2)
Definition: rtos.h:388
void vTaskResume(TaskHandle_t xTaskToResume)
void(* TaskFunction_t)(void *pvParameters)
Definition: rtos.h:110
EventBits_t xEventGroupWaitBits(const EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait)
EventBits_t xEventGroupGetBits(EventGroupHandle_t xEventGroup)
BaseType_t xTimerChangePeriod(TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xBlockTime)
uint32_t TickType_t
Definition: rtos.h:76
EventBits_t xEventGroupSetBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet)
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
uint32_t s_addr
Definition: rtos.h:790
#define UIP_HTONS(n)
Definition: uip.h:1248
EventBits_t xEventGroupSync(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait)
int ioctlsocket(int sockfd, int request, const void *opt)
static BaseType_t xEventGroupSetBitsFromISR(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:613
int poll_recv(int sockfd, uint32_t timeout)
void taskENTER_CRITICAL(void)
void vTaskStartScheduler(void)
TaskHandle_t xSemaphoreGetMutexHolder(SemaphoreHandle_t xSemaphore)
static BaseType_t xTimerChangePeriodFromISR(TimerHandle_t xTimer, TickType_t xNewPeriod, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:498
BaseType_t xTimerIsTimerActive(TimerHandle_t xTimer)
static BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t *const pxHigherPriorityTaskWoken)
Definition: rtos.h:362
int32_t BaseType_t
Definition: rtos.h:74
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait)
void vTaskCancelBlockingCall(TaskHandle_t xTaskToWakeup)
static BaseType_t xTaskResumeFromISR(TaskHandle_t xTaskToResume)
Definition: rtos.h:225
uint32_t socklen_t
Definition: rtos.h:774
static SemaphoreHandle_t xSemaphoreCreateRecursiveMutex(void)
Definition: rtos.h:305
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
static SemaphoreHandle_t xSemaphoreCreateCounting(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount)
Definition: rtos.h:287
static EventBits_t xEventGroupClearBitsFromISR(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear)
Definition: rtos.h:633
static BaseType_t xTimerPendFunctionCallFromISR(PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:564
struct DummySemaphoreQueue_t * SemaphoreHandle_t
Definition: rtos.h:253
TimerHandle_t xTimerCreate(const char *const pcTimerName, const TickType_t xTimerPeriod, const UBaseType_t uxAutoReload, void *const pvTimerID, TimerCallbackFunction_t pxCallbackFunction)
BaseType_t xTimerStart(TimerHandle_t xTimer, TickType_t xBlockTime)
BaseType_t xTimerDelete(TimerHandle_t xTimer, TickType_t xBlockTime)
static BaseType_t xTimerStopFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition: rtos.h:484
void vTaskDelete(TaskHandle_t xTask)
BaseType_t xTimerPendFunctionCall(PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait)
static void vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority)
Definition: rtos.h:200
static BaseType_t xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore, BaseType_t *const pxHigherPriorityTaskWoken)
Definition: rtos.h:334