summaryrefslogtreecommitdiffstats
path: root/peripheralservice/communication/server/src/CAN/CommWatch/CAN_CommWatch.cpp
blob: 2ece107209af8a86eec645a5f8109f9881ce7c77 (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
/*
 * @copyright Copyright (c) 2016-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.
 */

#include "CAN_CommWatch.h"

#include <native_service/frameworkunified_types.h>
#include <native_service/frameworkunified_framework_if.h>
#include <peripheral_service/Canif_API.h>
#include <utility>
#include <map>
#include <string>
#include "CAN_Thread.h"
#include "Thread_Common.h"

static CAN_CommWatchTable g_map_comm_watch_list_can;

void CANCommWatchInit(void) {
  g_map_comm_watch_list_can.clear();
  return;
}

static CAN_CommWatchTableIt CANCommWatchFind(CANID k, CAN_COMM_WATCH_VAL *val) {
  std::pair<CAN_CommWatchTableIt, CAN_CommWatchTableIt> range;
  CAN_CommWatchTableIt it;
  bool found = false;

  range = g_map_comm_watch_list_can.equal_range(k);
  for (it = range.first; it != range.second; it++) {
    if (!memcmp(&(it->second.notify_name),
                                  val->notify_name, sizeof(val->notify_name))) {
      found = true;
      break;
    }
  }

  if (!found)
    it = g_map_comm_watch_list_can.end();

  return it;
}

static bool CANCommWatchInsert(CANID k, CAN_COMM_WATCH_VAL *val) {
  CAN_CommWatchTableIt it = CANCommWatchFind(k, val);
  CAN_CommWatchTablePair p = std::make_pair((const CANID)k, *val);
  bool inserted = false;

  if (it == g_map_comm_watch_list_can.end()) {
    g_map_comm_watch_list_can.insert(p);
    inserted = true;
  }
  return inserted;
}

static void CANCommWatchUpdate(HANDLE h_app, CANID key,
                                             CAN_COMM_WATCH_VAL *val) {
  if (CANCommWatchInsert(key, val)) {
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "Can CommWatch : "
      "Entry (CANID=%x, DataID=%x, dst=%s, time=%d, cnt=%lu)",
      key, val->data_id,
      val->notify_name, val->set_time,
      g_map_comm_watch_list_can.size());
  } else {
    CAN_CommWatchTableIt it = CANCommWatchFind(key, val);
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "Can CommWatch : Update the time "
      "(CANID=%x, DataID=%x, dst=%s, time:%d -> %d, cnt =%lu)",
      key, val->data_id, val->notify_name,
      it->second.set_time, val->set_time, 
      g_map_comm_watch_list_can.size());
    it->second.comm_watch_flag = val->comm_watch_flag;
    it->second.set_time = val->set_time;
    it->second.timer_cnt = val->timer_cnt;
    it->second.data_id = val->data_id;
  }

  return;
}

static void CANCommWatchStop(HANDLE h_app, CANID key,
                                           CAN_COMM_WATCH_VAL *val) {
  CAN_CommWatchTableIt it = CANCommWatchFind(key, val);
  if (it != g_map_comm_watch_list_can.end()) {
    g_map_comm_watch_list_can.erase(it);
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "Can CommWatch : Stop the commwatch"
        "(CANID=%x, dst=%s, cnt=%lu)",
        key, val->notify_name, g_map_comm_watch_list_can.size());
  }

  return;
}

EFrameworkunifiedStatus CANCommWatch(HANDLE h_app) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __func__, "@@@ Start communication CanCommWatch");

  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
  CAN_COMM_WATCHEXT_MSG_DAT rcv_msg;
  CANID key;
  CAN_COMM_WATCH_VAL val;
  CAN_CommWatchTableIt it;

  memset(&rcv_msg, 0, sizeof(rcv_msg));
  memset(&val, 0, sizeof(val));
  e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &rcv_msg, sizeof(rcv_msg), eSMRRelease);
  if (e_status != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgDataOfSize Error(%d)", e_status);
    if (e_status == eFrameworkunifiedStatusInvldBufSize) {
      FrameworkunifiedClearMsgData(h_app);
    }
    goto cleanup;
  }

  // Create search data
  key = rcv_msg.ulCanid;
  snprintf(val.notify_name, sizeof(val.notify_name), "%s", rcv_msg.notifyName);
  val.data_id = rcv_msg.ulDid;
  val.comm_watch_flag = CAN_COMM_NORMAL;
  val.set_time = rcv_msg.usWatchTime;
  val.timer_cnt = rcv_msg.usWatchTime;
  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__,
         "CAN CommWatch : dst=%s, CANID=%x, dataID=%x, time=%d",
         rcv_msg.notifyName, 
         rcv_msg.ulCanid, 
         rcv_msg.ulDid, rcv_msg.usWatchTime);

  if (0 == rcv_msg.usWatchTime) {
    CANCommWatchStop(h_app, key, &val);
  } else {
    CANCommWatchUpdate(h_app, key, &val);
  }
  e_status = eFrameworkunifiedStatusOK;
cleanup:
  return e_status;
}

static EFrameworkunifiedStatus CANCommWatchSndMsg(HANDLE h_app,
                                        CAN_COMM_WATCH_VAL *v, uint32_t cid) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
  CAN_MSG_COMM_WATCHSTS msg;
  HANDLE h_client = NULL;

  memset(&msg, 0, sizeof(msg));
  msg.hdr.hdr.cid = (uint16_t)cid;
  msg.hdr.hdr.msgbodysize = sizeof(CAN_MSG_COMM_WATCHSTS_DAT);
  msg.hdr.hdr.rid = 0;
  msg.data.ulDid = v->data_id;

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__,  "cid=%x msgbodysize=%x ulDid=%x",
         msg.hdr.hdr.cid, msg.hdr.hdr.msgbodysize, msg.data.ulDid);

  h_client = CommonFindSender(h_app, v->notify_name);
  if (!h_client){
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CommonFindSender failed");
    goto cleanup;
  }

  e_status = FrameworkunifiedSendMsg(h_client, cid, sizeof(msg), &msg);
  if (e_status != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__,
      "FrameworkunifiedSendMSg Error(e_status:%d to %s)",
      e_status, v->notify_name);
    goto cleanup;
  }

  e_status = eFrameworkunifiedStatusOK;
cleanup:
  return e_status;
}

static void CANCommWatchTimeoutCore(HANDLE h_app, CAN_COMM_WATCH_VAL *v) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;

  if (CAN_COMM_NORMAL != v->comm_watch_flag)
    return;

  if (v->timer_cnt != 0)
    v->timer_cnt--;

  if (v->timer_cnt != 0)
    return;

  e_status = CANCommWatchSndMsg(h_app, v, CID_CAN_COMM_STOP);
  if (eFrameworkunifiedStatusOK == e_status) {
     v->comm_watch_flag = CAN_COMM_STOP;
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CANCommWatchSndMsg failed");
     // retry at next timeout.
  }
}

EFrameworkunifiedStatus CANCommWatchTimeout(HANDLE h_app) {
  CAN_CommWatchTableIt it;

  for (it = g_map_comm_watch_list_can.begin();
                            it != g_map_comm_watch_list_can.end(); it++) {
    CANCommWatchTimeoutCore(h_app, &(it->second));
  }

  return eFrameworkunifiedStatusOK;
}

static void CANCommWatchClearCore(HANDLE h_app, CANID id,
                                                CAN_COMM_WATCH_VAL *v) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;

  v->timer_cnt = v->set_time;

  if (CAN_COMM_STOP != v->comm_watch_flag)
    return;

  e_status = CANCommWatchSndMsg(h_app, v, CID_CAN_COMM_RESTORE);
  if (eFrameworkunifiedStatusOK == e_status) {
    v->comm_watch_flag = CAN_COMM_NORMAL;
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "Can CommWatch Clear : CANID=%x", id);
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CANCommWatchSndMsg failed");
     // retry at next timeout.
  }
}

void CANCommWatchClear(HANDLE h_app, CANID id) {
  std::pair<CAN_CommWatchTableIt, CAN_CommWatchTableIt> range;
  CAN_CommWatchTableIt it;

  range = g_map_comm_watch_list_can.equal_range(id);
  for (it = range.first; it != range.second; it++) {
    CANCommWatchClearCore(h_app, id, &(it->second));
  }
}