1 /** ----------------------------------------------------------------------
2  *
3  * Copyright (C) 2016 ST Microelectronics S.A.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *
18  ----------------------------------------------------------------------*/
19 #ifndef __HALCORE_PRIVATE_
20 #define __HALCORE_PRIVATE_
21 
22 #include <pthread.h>
23 #include <semaphore.h>
24 #include <stdint.h>
25 #include <time.h>
26 #include "halcore.h"
27 
28 #define MAX_NCIFRAME_PAYLOAD_SIZE 255
29 #define MAX_HEADER_SIZE 3
30 
31 #define MAX_BUFFER_SIZE (MAX_NCIFRAME_PAYLOAD_SIZE + MAX_HEADER_SIZE)
32 
33 // the three NCI bytes:
34 // Octet1 and Octet 2: connection-id and stuff
35 // Octet3: length of payload (in bytes)
36 
37 /* ----------------------------------------------------------------------------------------------*/
38 /* ----------------------------------------------------------------------------------------------*/
39 /* ----------------------------------------------------------------------------------------------*/
40 /* ----------------------------------------------------------------------------------------------*/
41 /* ----------------------------------------------------------------------------------------------*/
42 
43 #define HAL_QUEUE_MAX \
44   8 /* max. # of messages enqueued before going into blocking mode */
45 
46 /* thread messages  */
47 #define MSG_EXIT_REQUEST 0 /* worker thread should terminate itself */
48 #define MSG_TX_DATA 1      /* send a message downstream */
49 #define MSG_RX_DATA 2      /* a new message has arrived from lower layer */
50 
51 // HAL _WRAPPER
52 #define MSG_TX_DATA_TIMER_START 3
53 #define MSG_TIMER_START 4
54 
55 /* number of buffers used for incoming & outgoing data */
56 #define NUM_BUFFERS 10
57 
58 /* constants for the return value of osWait */
59 #define OS_SYNC_INFINITE 0xffffffffu
60 #define OS_SYNC_RELEASED 0
61 #define OS_SYNC_TIMEOUT 1
62 #define OS_SYNC_FAILED 0xffffffffu
63 
64 /* default timeouts */
65 #define HAL_SLEEP_TIMER 0
66 #define HAL_SLEEP_TIMER_DURATION 500 /* ordinary t1 timeout to resent data */
67 
68 typedef struct tagHalBuffer {
69   uint8_t data[MAX_BUFFER_SIZE];
70   size_t length;
71   struct tagHalBuffer* next;
72 } HalBuffer;
73 
74 typedef struct tagThreadMessage {
75   uint32_t command;    /* message type / command */
76   const void* payload; /* ptr to message related data item */
77   size_t length;       /* length of above payload */
78   HalBuffer* buffer;   /* buffer object (optional) */
79 } ThreadMesssage;
80 
81 typedef enum {
82   EVT_RX_DATA = 0,
83   EVT_TX_DATA = 1,
84   // HAL WRAPPER
85   EVT_TIMER = 2,
86 } HalEvent;
87 
88 typedef struct tagTimer {
89   struct timespec startTime; /* start time (CLOCK_REALTIME)       */
90   uint32_t duration;         /* timer duration in milliseconds    */
91   bool active;               /* true if timer is currently active */
92 } Timer;
93 
94 typedef struct tagHalInstance {
95   uint32_t flags;
96 
97   void* context;
98   HAL_CALLBACK callback;
99 
100   /* current timeout values */
101   uint32_t timeout;
102   Timer timer;
103 
104   /* threading and runtime support */
105   bool exitRequest;
106   sem_t semaphore;
107   pthread_t thread;
108   pthread_mutex_t hMutex; /* guards the message ringbuffer */
109 
110   /* IOBuffers for read/writes */
111   HalBuffer* bufferData;
112   HalBuffer* freeBufferList;
113   HalBuffer* pendingNciList; /* outgoing packages waiting to be processed */
114   HalBuffer* nciBuffer;      /* current buffer in progress */
115   sem_t bufferResourceSem;
116 
117   sem_t upstreamBlock;
118 
119   /* message ring-buffer */
120   ThreadMesssage ring[HAL_QUEUE_MAX];
121   int ringReadPos;
122   int ringWritePos;
123 
124   /* current frame going downstream */
125   uint8_t lastDsFrame[MAX_BUFFER_SIZE];
126   size_t lastDsFrameSize;
127 
128   /* current frame from CLF */
129   uint8_t lastUsFrame[MAX_BUFFER_SIZE];
130   size_t lastUsFrameSize;
131 
132 } HalInstance;
133 
134 #endif
135