• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 #include "halcore.h"
28 
29 #define MAX_NCIFRAME_PAYLOAD_SIZE 255
30 #define MAX_HEADER_SIZE 3
31 
32 #define MAX_BUFFER_SIZE (MAX_NCIFRAME_PAYLOAD_SIZE + MAX_HEADER_SIZE)
33 
34 // the three NCI bytes:
35 // Octet1 and Octet 2: connection-id and stuff
36 // Octet3: length of payload (in bytes)
37 
38 /* ----------------------------------------------------------------------------------------------*/
39 /* ----------------------------------------------------------------------------------------------*/
40 /* ----------------------------------------------------------------------------------------------*/
41 /* ----------------------------------------------------------------------------------------------*/
42 /* ----------------------------------------------------------------------------------------------*/
43 
44 #define HAL_QUEUE_MAX \
45   8 /* max. # of messages enqueued before going into blocking mode */
46 
47 /* thread messages  */
48 #define MSG_EXIT_REQUEST 0 /* worker thread should terminate itself */
49 #define MSG_TX_DATA 1      /* send a message downstream */
50 #define MSG_RX_DATA 2      /* a new message has arrived from lower layer */
51 
52 // HAL _WRAPPER
53 #define MSG_TX_DATA_TIMER_START 3
54 #define MSG_TIMER_START 4
55 
56 /* number of buffers used for incoming & outgoing data */
57 #define NUM_BUFFERS 10
58 
59 /* constants for the return value of osWait */
60 #define OS_SYNC_INFINITE 0xffffffffu
61 #define OS_SYNC_RELEASED 0
62 #define OS_SYNC_TIMEOUT 1
63 #define OS_SYNC_FAILED 0xffffffffu
64 
65 /* default timeouts */
66 #define HAL_SLEEP_TIMER 0
67 #define HAL_SLEEP_TIMER_DURATION 500 /* ordinary t1 timeout to resent data */
68 
69 typedef struct tagHalBuffer {
70   uint8_t data[MAX_BUFFER_SIZE];
71   size_t length;
72   struct tagHalBuffer* next;
73 } HalBuffer;
74 
75 typedef struct tagThreadMessage {
76   uint32_t command;    /* message type / command */
77   const void* payload; /* ptr to message related data item */
78   size_t length;       /* length of above payload */
79   HalBuffer* buffer;   /* buffer object (optional) */
80 } ThreadMessage;
81 
82 typedef enum {
83   EVT_RX_DATA = 0,
84   EVT_TX_DATA = 1,
85   // HAL WRAPPER
86   EVT_TIMER = 2,
87 } HalEvent;
88 
89 typedef struct tagTimer {
90   struct timespec startTime; /* start time (CLOCK_REALTIME)       */
91   uint32_t duration;         /* timer duration in milliseconds    */
92   bool active;               /* true if timer is currently active */
93 } Timer;
94 
95 typedef struct tagHalInstance {
96   uint32_t flags;
97 
98   void* context;
99   HAL_CALLBACK callback;
100 
101   /* current timeout values */
102   uint32_t timeout;
103   Timer timer;
104 
105   /* threading and runtime support */
106   bool exitRequest;
107   sem_t semaphore;
108   pthread_t thread;
109   pthread_mutex_t hMutex; /* guards the message ringbuffer */
110 
111   /* IOBuffers for read/writes */
112   HalBuffer* bufferData;
113   HalBuffer* freeBufferList;
114   HalBuffer* pendingNciList; /* outgoing packages waiting to be processed */
115   HalBuffer* nciBuffer;      /* current buffer in progress */
116   sem_t bufferResourceSem;
117 
118   sem_t upstreamBlock;
119 
120   /* message ring-buffer */
121   ThreadMessage ring[HAL_QUEUE_MAX];
122   int ringReadPos;
123   int ringWritePos;
124 
125   /* current frame going downstream */
126   uint8_t lastDsFrame[MAX_BUFFER_SIZE];
127   size_t lastDsFrameSize;
128 
129   /* current frame from CLF */
130   uint8_t lastUsFrame[MAX_BUFFER_SIZE];
131   size_t lastUsFrameSize;
132 
133 } HalInstance;
134 
135 #endif
136