summaryrefslogtreecommitdiffstats
path: root/positioning_hal/src/Common/positioning_common.cpp
blob: 00b3a8bcf5870a6435421f1fb56d4e4dde5c2712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
* @file positioning_common.cpp
*/

/*---------------------------------------------------------------------------*/
// Include files

#include "positioning_common.h"
#include "MDev_Gps_Common.h"
#include "MDev_GpsRecv.h"
#include "LineSensDrv_Thread.h"

/*---------------------------------------------------------------------------*/
// Value define

#define DATMOD_RETRY                        (3) /* Number of shared memory generation retries */
#define DATMOD_PREINIT                      (0) /* Shared Memory State [Before initialization] */
#define PRIM_NAME_MAX                       (32) /* Maximum Name Size */
#define VEHICLE_SHARE_NAME                  ("POS_VEHICLE_SHARE_MEMORY")  /* Shared memory name */

/* Mask for managing various notification reception conditions */
#define NTFY_MSK_NONE                       (0x00)
/* Service availability notification */
#define NTFY_MSK_COMMUNICATION_AVAILABILITY (0x01)
#define NTFY_MSK_PS_COMMUSB_AVAILABILITY    (0x02)
#define NTFY_MSK_PS_PSMSHADOW_AVAILABILITY  (0x04)
#define NTFY_MSK_CLOCK_AVAILABILITY         (0x08)
#define NTFY_MSK_NS_BACKUPMGR_AVAILABILITY  (0x10)
#define NTFY_MSK_SS_DEVDETSRV_AVAILABILITY  (0x20)
/* Other Notices */
#define NTFY_MSK_PS_PSMSHADOW_INIT_COMP     (0x01)  /* PSMShadow startup completion notice           */
#define NTFY_MSK_PS_LANSERVER_DEVICE_UPDATE (0x02)  /* LanServer device configuration change notification       */
#define NTFY_MSK_PS_LANSERVER_WAKEUP_COMP   (0x04)  /* LanServer start completion notice */

/* Thread state */
#define THREAD_STS_NOEXIST                  (0x00)
#define THREAD_STS_CREATING                 (0x01)
#define THREAD_STS_CREATED                  (0x02)

/*---------------------------------------------------------------------------*/
// ENUMERATION

/*---------------------------------------------------------------------------*/
// STRUCTURE

typedef struct StThreadInfo {
  EnumTID_POS     e_id;           /**< Thread ID */
  const int8_t*   p_name;         /**< Thread name  */
  PNO             p_no;           /**< Process number */
  CbFuncPtr       cb_routine;     /**< Start routine  */
  uint8_t         msk_available;  /**< Dependent services Availability  */
  uint8_t         msk_ntfy;       /**< Dependent notification                 */
  uint8_t         msk_thread;     /**< Dependent threads              */
  BOOL            b_is_depended;  /**< Positioning/Availability->TRUE change dependency */
  uint8_t         uc_status;      /**< Thread activation state                             */
  uint8_t         uc_order;       /**< Boot Sequence(Performance)                             */
  uint8_t         u_reserve[2];
} ST_THREAD_CREATE_INFO;

typedef struct {
    char  share_data_name[PRIM_NAME_MAX]; /**< Shared data name   */
    u_int32 data_size;            /**< Shared data size */
} ST_SHAREDATA;

static const int8_t kThreadNamePosMain[15]          = "POS_Main";
static const int8_t kThreadNamePosSens[15]          = "POS_Sens";
static const int8_t kThreadNamePosGps[15]           = "POS_Gps";
static const int8_t kThreadNamePosGpsRecv[15]       = "POS_Gps_Recv";
static const int8_t kThreadNamePosGpsRollover[15]   = "POS_Gps_Rolovr";

static ST_THREAD_CREATE_INFO g_pos_thread_create_info[] = {
  { /* POS_Main */
    ETID_POS_MAIN, /* (1) */
    NULL, /* (2) */
    0, /* (3) */
    NULL, /* (4) */
    (NTFY_MSK_NONE), /* (5) */
    (NTFY_MSK_NONE), /* (6) */
    0, /* (7) */
    FALSE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
  { /* POS_sens */
    ETID_POS_SENS, /* (1) */
    kThreadNamePosSens, /* (2) */
    PNO_LINE_SENS_DRV, /* (3) */
    &LineSensDrvThread, /* (4) */
    (NTFY_MSK_NONE), /* (5) */
    (NTFY_MSK_NONE), /* (6) */
    0, /* (7) */
    FALSE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
  { /* POS_Gps */
    ETID_POS_GPS, /* (1) */
    kThreadNamePosGps, /* (2) */
    PNO_NAVI_GPS_MAIN, /* (3) */
    &DevGpsMainThread, /* (4) */
    (NTFY_MSK_NONE), /* (5) */
    (NTFY_MSK_NONE), /* (6) */
    0, /* (7) */
    TRUE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
  { /* POS_Gps_Recv */
    ETID_POS_GPS_RECV, /* (1) */
    kThreadNamePosGpsRecv, /* (2) */
    PNO_NAVI_GPS_RCV, /* (3) */
    &DevGpsRecvThread, /* (4) */
    (NTFY_MSK_NONE), /* (5) */
    (NTFY_MSK_NONE), /* (6) */
    THREAD_STS_MSK_POS_GPS, /* (7) */
    FALSE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
  { /* POS_Gps_Rolovr */
    ETID_POS_GPS_ROLLOVER, /* (1) */
    NULL, /* (2) */
    0, /* (3) */
    NULL, /* (4) */
    (NTFY_MSK_NONE), /* (5) */
    (NTFY_MSK_NONE), /* (6) */
    0, /* (7) */
    FALSE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
  { /* Termination */
    ETID_POS_MAX, /* (1) */
    NULL, /* (2) */
    0, /* (3) */
    NULL, /* (4) */
    NTFY_MSK_NONE, /* (5) */
    NTFY_MSK_NONE, /* (6) */
    0, /* (7) */
    FALSE, /* (8) */
    THREAD_STS_NOEXIST, /* (9) */
    0 /* (10) */
  },
};

static ST_SHAREDATA g_sharedata_tbl[] = {
    /* Shared data name to be generated,        Shared data size */
    { {VEHICLE_SHARE_NAME},         512 * 11 }, /* Vehicle sensor information acquisition                     */
#if 0 /* Not used in _CWORD71_ below */
    { {"SENSOR_SHARE_MEMORY"},        512 * 11 }, /* Vehicle sensor information Pkg acquisition                  */
    { {"GPS_INT_SIGNAL_SHARE_MEMORY"},    4    }, /* GPS interrupt signal acquisition                      */
    { {"LOG_SETTING_SHARE_MEMORY"},     36     }, /* DR feature log acquisition                         */
    { {"GYRO_CONNECT_STTS_SHARE_MEMORY"}, 4    }, /* Gyro connection status acquisition                   */
    { {"EPHEMERIS_NUM_SHARE_MEMORY"},   4    }, /* Effective ephemeris count acquisition at shutdown */
    { {"LOCALTIME_SHARE_MEMORY"},     12     }, /* Local time acquisition at shutdown     */
    { {"LONLAT_SHARE_MEMORY"},        8    }, /* Location acquisition at shutdown               */
#endif
    { {(int8)NULL},             0    }  /* Termination */
};


/*---------------------------------------------------------------------------*/
// Functions

static EFrameworkunifiedStatus PosStopThreadDummy(HANDLE h_app) {
  return eFrameworkunifiedStatusOK;
}

static uint32_t PosGetMsg(HANDLE h_app, void** p_buf, uint32_t ul_size) {
  EFrameworkunifiedStatus  e_status = eFrameworkunifiedStatusOK;
  uint32_t    tmp_size = 0;
  void*       p_rev_buf = NULL;

  if ((h_app == NULL) || (p_buf == NULL)) {
      POSITIONING_LOG("Argument ERROR!! [h_app = %p, pBuf = %p]", h_app , p_buf);
  } else {
    /* Check the size of received data */
    tmp_size = FrameworkunifiedGetMsgLength(h_app);
    if (tmp_size > ul_size) {
      POSITIONING_LOG("Message ul_size ERROR!! [tmp_size = %d, maxsize = %d]", tmp_size, ul_size);
      tmp_size = 0;
    } else {
      /* Obtain data */
      e_status = FrameworkunifiedGetDataPointer(h_app, &p_rev_buf);

      if (e_status == eFrameworkunifiedStatusOK) {
        *p_buf = p_rev_buf;
      } else if (e_status == eFrameworkunifiedStatusInvldBufSize) {
        e_status = FrameworkunifiedGetMsgDataOfSize(h_app, *p_buf, tmp_size);

        if (e_status != eFrameworkunifiedStatusOK) {
          POSITIONING_LOG("FrameworkunifiedGetMsgDataOfSize ERROR [e_status:%d]", e_status);
          tmp_size = 0;
        }
      } else {
        POSITIONING_LOG("FrameworkunifiedGetDataPointer ERROR [e_status:%d]", e_status);
        tmp_size = 0;
      }
    }
  }

  return tmp_size;
}

static void PosCreateSharedMemory(void) {
  RET_API ret_api = RET_NORMAL;
  void    *mod_exec_dmy; /* Module data pointer(dummy) */
  int         retry; /* Retry counter */
  ST_SHAREDATA *p_shm_tbl;

  /* Configure Shared Data Generating Tables */
  p_shm_tbl = g_sharedata_tbl;

  while (*(p_shm_tbl->share_data_name) != (int8)NULL) {
      for (retry = 0; retry < DATMOD_RETRY; retry++) {
          /* Shared Memory Generation */
          ret_api = _pb_CreateShareData(p_shm_tbl->share_data_name, p_shm_tbl->data_size, &mod_exec_dmy);
          if (ret_api == RET_NORMAL) {
              /* Set the shared memory status flag to "Before initialization (0)" */
              *reinterpret_cast<u_int32 *>(mod_exec_dmy) = DATMOD_PREINIT;
              break;
          } else {
              /* Error Handling */
              POSITIONING_LOG("_pb_CreateShareData ERROR [ret_api:%d]", ret_api);
          }
      }

      if (retry >= DATMOD_RETRY) {
          POSITIONING_LOG("_pb_CreateShareData failed more %d times.", DATMOD_RETRY);
          _pb_Exit();
          /* don't arrive here. */
      }

      /* Next shared memory generation */
      p_shm_tbl++;
  }
  return;
}

EFrameworkunifiedStatus PosInitialize(HANDLE h_app) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  RET_API ret_api;
  ret_api = _pb_Setup_CWORD64_API(h_app);
  if (ret_api != RET_NORMAL) {
      POSITIONING_LOG("_pb_Setup_CWORD64_API ERROR!! [ret_api = %d]", ret_api);

      e_status = eFrameworkunifiedStatusFail;
  } else {
    PosCreateSharedMemory();
  }
  return e_status;
}

/**
 * @brief
 *   Common processing after thread startup
 *
 *   Thread naming, message queue creation, thread startup response
 *
 * @param[in]  h_app Application handle
 * @param[in]  eTid	Thread ID
 *
 * @return  Thread startup mode
 */
EnumSetupMode_POS PosSetupThread(HANDLE h_app, EnumTID_POS e_tid) {
  RET_API                 ret = RET_NORMAL;
  ST_THREAD_CREATE_INFO*  p_thread_info = g_pos_thread_create_info;
  ST_THREAD_CREATE_INFO*  p_info = p_thread_info + e_tid;
  ST_THREAD_SETUP_INFO    st_setup_info;
  ST_THREAD_SETUP_INFO*   pst_setup_info = &st_setup_info;
  /* Application handle setting */
  _pb_SetAppHandle(h_app);
  /* Create Message Queue */
  _pb_CreateMsg(p_info->p_no);
  /* Get thread startup information */
  pst_setup_info->mode = EPOS_SETUP_MODE_NORMAL;
  (void)PosGetMsg(h_app, reinterpret_cast<void**>(&pst_setup_info), sizeof(ST_THREAD_SETUP_INFO));

  POSITIONING_LOG("[mode = %d]", pst_setup_info->mode);

  /* Issue thread creation completion notice */
  ret = _pb_SndMsg_Ext(POS_THREAD_NAME,
                        CID_THREAD_CREATE_COMP,
                        sizeof(EnumTID_POS),
                        reinterpret_cast<const void*>(&e_tid),
                        0);

  if (ret != RET_NORMAL) {
    POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [ret = %d]", ret);
  }

  return pst_setup_info->mode;
}

/**
 * @brief
 *   Common processing at thread stop
 *
 *   Thread stop response, thread destruction
 *
 * @param[in]  e_tid	Thread ID
 */
void PosTeardownThread(EnumTID_POS e_tid) {
  RET_API e_ret = RET_NORMAL;
  ST_THREAD_CREATE_INFO*  p_thread_info = g_pos_thread_create_info;

  (p_thread_info + e_tid)->uc_status = THREAD_STS_NOEXIST;
  /* Issue thread stop completion notice */
  e_ret = _pb_SndMsg_Ext(POS_THREAD_NAME,
                           CID_THREAD_STOP_COMP,
                           sizeof(EnumTID_POS),
                           reinterpret_cast<const void*>(&e_tid),
                           0);

  if (e_ret != RET_NORMAL) {
    POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [e_ret = %d]", e_ret);
  }

  /* Thread destruction */
  _pb_ExitThread((DWORD)0);

  return;
}

/**
 * @brief
 *   Positioning in-process thread creation
 *
 * @param[in]  hApp Application handle
 */
EFrameworkunifiedStatus PosCreateThread(HANDLE h_app, int8_t index) {
  HANDLE                  thread_handle;
  EFrameworkunifiedStatus              e_status = eFrameworkunifiedStatusOK;
  ST_THREAD_CREATE_INFO*  p_thread_info = g_pos_thread_create_info;

  p_thread_info += index;

  static EnumSetupMode_POS g_setup_mode;

  if (NULL == h_app) {
    return eFrameworkunifiedStatusInvldHandle;
  }

  if (index < ETID_POS_MAX) {
    if ((p_thread_info->uc_status == THREAD_STS_NOEXIST) && (p_thread_info->cb_routine != NULL)) {
      /* Thread creation */
      thread_handle = FrameworkunifiedCreateChildThread(h_app,
                                           (PCSTR)(p_thread_info->p_name),
                                           p_thread_info->cb_routine,
                                           &PosStopThreadDummy);

      if (thread_handle == NULL) {
        POSITIONING_LOG("FrameworkunifiedCreateChildThread ERROR!! [tHandle=%p]", thread_handle);
      } else {
        e_status = FrameworkunifiedStartChildThread(h_app,
                                       thread_handle,
                                       sizeof(EnumSetupMode_POS),
                                       &g_setup_mode);

        if (e_status != eFrameworkunifiedStatusOK) {
          POSITIONING_LOG("FrameworkunifiedStartChildThread ERROR!! [e_status=%d, name=%s]", e_status, p_thread_info->p_name);
        } else {
          p_thread_info->uc_status = THREAD_STS_CREATING;
          POSITIONING_LOG("name=%s\n", p_thread_info->p_name);
        }
      }
    }
  }
  return e_status;
}

/*---------------------------------------------------------------------------*/
/*EOF*/