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 
54 /* number of buffers used for incoming & outgoing data */
55 #define NUM_BUFFERS 10
56 
57 /* constants for the return value of osWait */
58 #define OS_SYNC_INFINITE 0xffffffffu
59 #define OS_SYNC_RELEASED 0
60 #define OS_SYNC_TIMEOUT 1
61 #define OS_SYNC_FAILED 0xffffffffu
62 
63 /* default timeouts */
64 #define HAL_SLEEP_TIMER 0
65 #define HAL_SLEEP_TIMER_DURATION 500 /* ordinary t1 timeout to resent data */
66 
67 typedef struct tagHalBuffer {
68     uint8_t data[MAX_BUFFER_SIZE];
69     size_t length;
70     struct tagHalBuffer* next;
71 } HalBuffer;
72 
73 typedef struct tagThreadMessage {
74     uint32_t command;    /* message type / command */
75     const void* payload; /* ptr to message related data item */
76     size_t length;       /* length of above payload */
77     HalBuffer* buffer;   /* buffer object (optional) */
78 } ThreadMesssage;
79 
80 typedef enum {
81     EVT_RX_DATA = 0,
82     EVT_TX_DATA = 1,
83     // HAL WRAPPER
84     EVT_TIMER = 2,
85 } HalEvent;
86 
87 typedef struct tagTimer {
88     struct timespec startTime; /* start time (CLOCK_REALTIME)       */
89     uint32_t duration;         /* timer duration in milliseconds    */
90     bool active;               /* true if timer is currently active */
91 } Timer;
92 
93 typedef struct tagHalInstance {
94     uint32_t flags;
95 
96     void* context;
97     HAL_CALLBACK callback;
98 
99     /* current timeout values */
100     uint32_t timeout;
101     Timer timer;
102 
103     /* threading and runtime support */
104     bool exitRequest;
105     sem_t semaphore;
106     pthread_t thread;
107     pthread_mutex_t hMutex; /* guards the message ringbuffer */
108 
109     /* IOBuffers for read/writes */
110     HalBuffer* bufferData;
111     HalBuffer* freeBufferList;
112     HalBuffer* pendingNciList; /* outgoing packages waiting to be processed */
113     HalBuffer* nciBuffer;      /* current buffer in progress */
114     sem_t bufferResourceSem;
115 
116     sem_t upstreamBlock;
117 
118     /* message ring-buffer */
119     ThreadMesssage ring[HAL_QUEUE_MAX];
120     int ringReadPos;
121     int ringWritePos;
122 
123     /* current frame going downstream */
124     uint8_t lastDsFrame[MAX_BUFFER_SIZE];
125     size_t lastDsFrameSize;
126 
127     /* current frame from CLF */
128     uint8_t lastUsFrame[MAX_BUFFER_SIZE];
129     size_t lastUsFrameSize;
130 
131 } HalInstance;
132 
133 #endif
134