HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
hipcsvs_rt.c
Go to the documentation of this file.
1 #include <string.h>
2 
3 #include <hipc/scfg.h>
4 #include <hipc/usoc.h>
5 #include <hipc/usocutils.h>
6 
7 #include "CFGrt.h"
8 #include "CFGrt_scfg.h"
9 #include "hipcsvs_rt.h"
10 
11 #define PBANK_PAGE_SIZE sizeof(struct STRUprm)
12 #define QUEUE_ITEM_SIZE sizeof(struct STRUlog)
13 #define QUEUE_N_SLOT 4
14 
17 
18 void hipcsvsRtInit(void)
19 {
20  static struct STRUprm bufprm[2];
21  static struct STRUlog buflog[QUEUE_N_SLOT];
22 
23  bufprm[0].size = 0;
25 
27  QUEUE_N_SLOT);
28 
29  return;
30 }
31 
32 static enum HIPC_errno send_cast(struct HIPC_usoc *const so_p)
33 {
34  enum HIPC_errno retval;
35  const struct STRUlog *log_p;
36  struct HIPC_queue *const queue_p = &hipcsvsRt_queue;
37  static const unsigned char loghdr[HIPC_MSGHDR_SIZE] = {
38  HIPC_MESSAGE_TYPE_CAST, HIPCUD_STRUlog, 0, QUEUE_ITEM_SIZE
39  };
40 
41  retval = HIPC_SUCCESS;
42 
43  log_p = hipcQueueEmpty(queue_p);
44  if (NULL != log_p) {
45  retval = hipcUsocWrite(so_p, loghdr, HIPC_MSGHDR_SIZE);
46  if (HIPC_SUCCESS != retval) {
47  return retval;
48  }
49  retval = hipcUsocWrite(so_p, log_p, QUEUE_ITEM_SIZE);
50  if (HIPC_SUCCESS != retval) {
51  return retval;
52  }
53  hipcQueuePop(queue_p);
54  }
55  return retval;
56 }
57 
58 enum HIPC_errno hipcsvsRtRun(struct HIPC_usoc *const so_p,
59  hipc_msgd * const mdq_p)
60 {
61  hipc_msgd mdR;
62  hipc_msgd mdS;
63  enum HIPC_errno retval;
64  struct HIPC_pbank *const pbank_p = &hipcsvsRt_pbank;
65  struct STRUprm *prm_p;
66  int rready;
67  uint8_t i;
68 
69  for (;;) {
70  do {
71  rready = hipcUsocReadReady(so_p);
72  if (-1 == rready) {
73  return hipcGenmdQuit(mdq_p, HIPC_ERR_S_IO_READ, "Ready");
74  }
75  if (0 == rready) {
76  retval = send_cast(so_p);
77  if (HIPC_SUCCESS != retval) {
78  return hipcGenmdQuit(mdq_p, retval, "CAST0");
79  }
80  }
81  } while (0 == rready);
82 
83  retval =
85  if (HIPC_SUCCESS != retval) {
86  return hipcGenmdQuit(mdq_p, retval, "Header");
87  }
88 
89  switch (hipcMsgdType(&mdR)) {
91  return hipcGenmdQuit(mdq_p, HIPC_SUCCESS, NULL);
93  if (HIPCUD_STRUprm != hipcMsgdStruno(&mdR)) {
95  "GETc0");
96  }
97  if (HIPC_PBANK_STAT_GROUND != hipcPbankGetStat(pbank_p)) {
99  "GETc1");
100  }
101  retval =
102  hipcGenmdSuccessToGet(&mdS, &mdR,
103  hipcPbankGetWritablePage(pbank_p),
105  if (HIPC_SUCCESS != retval) {
106  return hipcGenmdQuit(mdq_p, retval, "GETr");
107  }
108  break;
110  if (HIPCUD_STRUprm != hipcMsgdStruno(&mdR)) {
111  return hipcGenmdQuit(mdq_p, HIPC_ERR_S_INV_STRUNO,
112  "PUTc0");
113  }
114  if (HIPC_PBANK_STAT_GROUND != hipcPbankGetStat(pbank_p)) {
116  "PUTc1");
117  }
118 
119  prm_p = hipcPbankGetWritablePage(pbank_p);
120  retval = hipcMsgdSetBdyForPut(&mdR, prm_p, PBANK_PAGE_SIZE);
121  if (HIPC_SUCCESS != retval) {
122  return hipcGenmdQuit(mdq_p, retval, "PUTs");
123  }
124  retval = hipcUsocRead(so_p, hipcMsgdBdyPtr(&mdR),
125  hipcMsgdBdySize(&mdR));
126  if (HIPC_SUCCESS != retval) {
127  return hipcGenmdQuit(mdq_p, retval, "PUTb");
128  }
129 
130  if (PTRN_SIZE_MAX < prm_p->size) {
131  return hipcGenmdQuit(mdq_p, HIPC_ERR_S_INV_UDATA,
132  "PTRN_SIZE_MAX");
133  }
134  for (i = 0; i < prm_p->size; ++i) {
135  if (1 < prm_p->pattern[i]) {
136  return hipcGenmdQuit(mdq_p, HIPC_ERR_S_INV_UDATA,
137  "1 < prm_p->pattern[i]");
138  }
139  }
140 
141  retval = hipcPbankSwitchRequest(pbank_p);
142  if (HIPC_SUCCESS != retval) {
143  return hipcGenmdQuit(mdq_p, retval, "pbank0");
144  }
145  while (HIPC_PBANK_STAT_REQUESTED == hipcPbankGetStat(pbank_p)) {
146  retval = send_cast(so_p);
147  if (HIPC_SUCCESS != retval) {
148  return hipcGenmdQuit(mdq_p, retval, "CAST1");
149  }
150  }
151  retval = hipcPbankSwitchComplete(pbank_p);
152  if (HIPC_SUCCESS != retval) {
153  return hipcGenmdQuit(mdq_p, retval, "pbank1");
154  }
155 
156  hipcGenmdSuccess(&mdS);
157  break;
158  default:
159  return hipcGenmdQuit(mdq_p, HIPC_ERR_S_INV_MSG_TYPE, NULL);
160  }
161  retval = hipcUsocWriteMsgd(so_p, &mdS);
162  if (HIPC_SUCCESS != retval) {
163  return hipcGenmdQuit(mdq_p, retval, "RESP");
164  }
165  }
166 }
167 
168 void hipcMain(struct HIPC_usoc *const so_p)
169 {
170  hipc_msgd mdQ;
171  enum HIPC_errno retval;
172 
174 
175  retval = hipcUsocAcceptSimple(so_p, &HIPCUD_CFGrt_SCFG);
176  if (HIPC_SUCCESS != retval) {
177  hipcGenmdQuit(&mdQ, retval, "Failed to accept");
178  } else {
179  retval = hipcsvsRtRun(so_p, &mdQ);
180  }
181 
182  if ((HIPC_ERR_S_IO_READ != retval) && (HIPC_ERR_S_IO_WRITE != retval)) {
183  hipcUsocWriteMsgd(so_p, &mdQ);
184  }
185 
186  return;
187 }
enum HIPC_errno hipcUsocAcceptSimple(struct HIPC_usoc *const so_p, const struct HIPC_scfg *const cfg_p)
Accepts a HIPC session of cfg_p.
Definition: usocutils.c:141
void * hipcPbankGetWritablePage(struct HIPC_pbank *const bank_p)
Gets a current writable page.
Definition: pbank.c:55
enum HIPC_errno hipcUsocRead(struct HIPC_usoc *so_p, void *buf, const size_t size)
Reads exactly size bytes from so_p.
Definition: posix_usoc.c:24
enum HIPC_errno hipcGenmdSuccessToGet(hipc_msgd *const mds_p, hipc_msgd const *const mdr_p, void *const stru_p, const size_t stru_size)
Initializes mds_p as a response to mdr_p whose type must be GET.
Definition: msgd.c:102
#define QUEUE_ITEM_SIZE
Definition: hipcsvs_rt.c:12
enum HIPC_errno hipcPbankSwitchRequest(struct HIPC_pbank *const bank_p)
Requests to switch pages.
Definition: pbank.c:77
enum HIPC_errno hipcsvsRtRun(struct HIPC_usoc *const so_p, hipc_msgd *const mdq_p)
Definition: hipcsvs_rt.c:25
enum HIPC_errno hipcUsocWriteMsgd(struct HIPC_usoc *const so_p, hipc_msgd const *const md_p)
Writes md_p to so_p.
Definition: usocutils.c:14
enum HIPC_errno hipcUsocWrite(struct HIPC_usoc *so_p, const void *buf, const size_t size)
Writes exactly size bytes to so_p.
Definition: posix_usoc.c:36
void hipcQueueInit(struct HIPC_queue *const que_p, void *const buf_p, const size_t item_size, const size_t n_slot)
Initializes a queue.
Definition: queue.c:12
HIPC_errno
Definition: hipc.h:4
unsigned char hipcMsgdStruno(hipc_msgd const *const md_p)
Returns structure number of a HIPC message data.
Definition: msgd.c:31
enum HIPC_errno hipcMsgdSetBdyForPut(hipc_msgd *const mdr_p, void *const stru_p, const size_t stru_size)
Sets a body to a HIPC message data that contains a header of a PUT message.
Definition: msgd.c:140
void hipcQueuePop(struct HIPC_queue *const que_p)
Pops an item and releases a slot.
Definition: queue.c:61
unsigned char hipcMsgdBdySize(hipc_msgd const *const md_p)
Returns body size of a HIPC message data.
Definition: msgd.c:9
#define hipcGenmdQuit(md_p, hipc_errno, errdtlstr)
Initializes a HIPC message data as a QUIT message.
Definition: msgd.h:26
#define hipcGenmdSuccess(md_p)
Initializes a HIPC message data as a SUCCESS message.
Definition: msgd.h:35
struct HIPC_queue hipcsvsRt_queue
Definition: hipcsvs_rt.c:16
enum HIPC_pbank_stat hipcPbankGetStat(struct HIPC_pbank *const bank_p)
Gets current status of a bank.
Definition: pbank.c:66
Definition: usoc.h:4
int hipcUsocReadReady(struct HIPC_usoc *so_p)
Definition: main_posix.c:14
void hipcPbankInit(struct HIPC_pbank *const bank_p, void *const buf_p, const size_t page_size)
Initializes a parameter bank.
Definition: pbank.c:14
static enum HIPC_errno send_cast(struct HIPC_usoc *const so_p)
Definition: hipcsvs_rt.c:32
#define hipcMsgdBdyPtr(md_p)
Returns a pointer to a body of a HIPC message data.
Definition: msgd.h:52
#define PBANK_PAGE_SIZE
Definition: hipcsvs_rt.c:11
unsigned char hipcMsgdType(hipc_msgd const *const md_p)
Returns type of a HIPC message data.
Definition: msgd.c:20
void * hipcQueueEmpty(struct HIPC_queue *const que_p)
Returns an address of the head item if it exists.
Definition: queue.c:44
void hipcsvsRtInit(void)
Definition: hipcsvs_rt.c:15
#define hipcMsgdHdrPtr(md_p)
Returns a pointer to a header of a HIPC message data.
Definition: msgd.h:44
enum HIPC_errno hipcPbankSwitchComplete(struct HIPC_pbank *const bank_p)
Makes both pages have the same latest contents and completes the switching.
Definition: pbank.c:121
struct HIPC_pbank hipcsvsRt_pbank
Definition: hipcsvs_rt.c:13
void hipcMain(struct HIPC_usoc *const so_p)
Definition: hipcsvs_rt.c:104