summaryrefslogtreecommitdiffstats
path: root/communication/server/src/CAN/Delivery/CAN_DeliveryData.cpp
blob: a0bc1f0f10b0794f1f47f3c6a18a4ecf09ae5523 (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
/*
 * @copyright Copyright (c) 2016-2019 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		:CAN_DeliveryData.cpp
 *	SYSTEM		:_CWORD107_
 *	SUBSYSTEM	:EXL process
 *	PROGRAM	:CAN thread CAN data delivery data management
 *	Module configuration	:CANDeliveryDataInit( )					CANDataDelivery Management Data Initialization Process
 *                    CAN_DeliveryCanidCheck( ) 				CAN ID registration and delivery registration checking process
 *                    CAN_DeliveryListCapacityCheck( ) 			CANDataDistribution destination management table free space confirmation processing
 *                    CAN_DeliveryCanidEntryCheck( )			CANDataChecking the status of delivery CAN ID control table entries
 *                    CANDeliveryEntry( )						CANDataDelivery registration process
 *                    CAN_SndListGenerate( ) 					CANData ID List Creation Process for Delivery
 ******************************************************************************/
#include "CAN_DeliveryData.h"

#include <string.h>  // NOLINT(build/include)
#include <native_service/frameworkunified_framework_if.h>
#include <other_service/strlcpy.h>

#include <utility>
#include <string>
#include <map>

#include "CAN_Thread.h"
#include "CAN_Delivery.h"
#include "Canif_API_Local.h"
//#include "can_hal.h"

/*************************************************/
/*           Global variables                      */
/*************************************************/
std::multimap<CANID, std::string> g_map_delivery_list;
std::multimap<uint16_t, std::string> g_map__CWORD29__delivery_list;

/*******************************************************************************
 * MODULE    : CANDeliveryDataInit
 * ABSTRACT  : CANDataDelivery Management Data Initialization Process
 * FUNCTION  : CANDataInitialize delivery management data for 
 * ARGUMENT  : void
 * NOTE      : 
 * RETURN    : void
 ******************************************************************************/
void CANDeliveryDataInit(void) {
  return;
}

/*******************************************************************************
 * MODULE    : CANDeliveryEntry
 * ABSTRACT  : CANDataDelivery registration process
 * FUNCTION  : CANDataRegister delivery 
 * ARGUMENT  : notifyId	: Addresses for delivery ID
 *             usCanNum		: Registered number
 *             *pulCanid	: CAN ID array pointers
 * NOTE      : 
 * RETURN    : void
 ******************************************************************************/
EFrameworkunifiedStatus CANDeliveryEntry(HANDLE h_app) {
  uint16_t i;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  CAN_DELIVERY_ENTRY rcv_msg;
  std::string notify_name;
  std::multimap<CANID, std::string>::iterator it;

  e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &rcv_msg, sizeof(rcv_msg), eSMRRelease);  // LCOV_EXCL_BR_LINE 200: unexpect branch // NOLINT (whitespace/line_length)
  if (e_status != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 4: NSFW error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "#CAN_thread# CAN DeliveryEntry FrameworkunifiedGetMsgDataOfSize Error");  // LCOV_EXCL_LINE 4: NSFW error case // NOLINT (whitespace/line_length)
    goto exit;  // LCOV_EXCL_LINE 4: NSFW error case
  }

  for (i = 0; (i < rcv_msg.usCanNum) && (i < static_cast<uint16_t>(CAN_DELIVERY_CANID_ENTRY_MAX)); i++) {
    // Check for duplicate data
    // If the data to be registered is duplicated, an eFrameworkunifiedStatusOK is returned and the data is not registered.
    bool isDuplication = false;
    it = g_map_delivery_list.find(static_cast<std::multimap<CANID, std::string>::key_type>(rcv_msg.ulCanid[i]));
    for (; it != g_map_delivery_list.end(); ++it) {
      if (it->second == rcv_msg.notifyName) {
        isDuplication = true;
        break;
      }
    }
    if (isDuplication) {
      continue;
    }

    if ((uint32_t)g_map_delivery_list.size() >= CAN_DELIVERY_LIST_NUM) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "#CAN_thread# CAN DeliveryEntry Table MAX size over");  // LCOV_EXCL_BR_LINE 15: marco defined in "native_service/ns_logger_if.h" // NOLINT (whitespace/line_length)
      e_status = eFrameworkunifiedStatusFail;
      break;
    }
    FRAMEWORKUNIFIEDLOG(ZONE_ENTRY, "", "[CAN] %04x %s", rcv_msg.ulCanid[i], rcv_msg.notifyName);  // LCOV_EXCL_LINE 4: NSFW error case // NOLINT (whitespace/line_length)
    notify_name = rcv_msg.notifyName;
    g_map_delivery_list.insert(std::make_pair(rcv_msg.ulCanid[i], notify_name));  // LCOV_EXCL_BR_LINE 200: unexpect branch // NOLINT (whitespace/line_length)
  }

exit:
  return e_status;
}

/*******************************************************************************
 * MODULE    : CAN_CWORD29_DeliveryEntry
 * ABSTRACT  : Process of registering the delivery of _CWORD29_ data
 * FUNCTION  : Register the delivery of _CWORD29_ data 
 * ARGUMENT  : h_app      : HANDLE
 * NOTE      :
 * RETURN    : EFrameworkunifiedStatus
 ******************************************************************************/
EFrameworkunifiedStatus CAN_CWORD29_DeliveryEntry(HANDLE h_app) {
  uint16_t i;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  CAN__CWORD29__DELIVERY_ENTRY rcv_msg;
  std::string notify_name;
  std::multimap<uint16_t, std::string>::iterator it;

  if (reinterpret_cast<HANDLE>(NULL) == h_app) {
    return eFrameworkunifiedStatusFail;
  }

  e_status = FrameworkunifiedGetMsgDataOfSize(h_app, reinterpret_cast<PVOID>(&rcv_msg),
                                 static_cast<UI_32>(sizeof(CAN__CWORD29__DELIVERY_ENTRY)), eSMRRelease);
  if (e_status != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "#CAN_thread# CAN DeliveryEntry FrameworkunifiedGetMsgDataOfSize Error");
    return e_status;
  }

  if (CAN_DELIVERY_OPC_ENTRY_MAX <= rcv_msg.usOpcNum) {
    e_status = eFrameworkunifiedStatusFail;
    return e_status;
  }

  for (i = 0; i < rcv_msg.usOpcNum; i++) {
    // Check for duplicate data
    // If the data to be registered is duplicated, an eFrameworkunifiedStatusOK is returned and the data is not registered.
    bool isDuplication = false;
    it = g_map__CWORD29__delivery_list.find(static_cast<std::multimap<uint16_t, std::string>::key_type>(rcv_msg.usOpc[i]));
    for (; it != g_map__CWORD29__delivery_list.end(); ++it) {
      if (it->second == rcv_msg.notifyName) {
        isDuplication = true;
        break;
      }
    }
    if (isDuplication) {
      continue;
    }

    if (static_cast<uint32_t>(g_map__CWORD29__delivery_list.size()) >= CAN_DELIVERY_LIST_NUM) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "#CAN_thread# _CWORD29_DeliveryEntry Table MAX size over");
      e_status = eFrameworkunifiedStatusFail;
      break;
    }
    FRAMEWORKUNIFIEDLOG(ZONE_ENTRY, "", "[OPC] %04x %s", rcv_msg.usOpc[i], rcv_msg.notifyName);
    notify_name = rcv_msg.notifyName;
    g_map__CWORD29__delivery_list.insert(std::make_pair(rcv_msg.usOpc[i], notify_name));
  }

  return e_status;
}

/*******************************************************************************
 * MODULE    : CANDeliveryBufferOut
 * ABSTRACT  : CAN shipping table log output processing
 * FUNCTION  : Output the CAN shipping table log 
 * ARGUMENT  : FILE *fp_log		: File pointer of the log output file
 * NOTE      : 
 * RETURN    : Thread ID
 ******************************************************************************/
void CANDeliveryBufferOut(FILE *fp_log) {  // LCOV_EXCL_START 8:dead code
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  uint32_t usage_rate;
  std::multimap<CANID, std::string>::iterator it;

  if (NULL != fp_log) {
    /* Destination management table output */
    usage_rate = (((uint32_t)g_map_delivery_list.size() * 100) / CAN_DELIVERY_LIST_NUM);
    (void)fprintf(fp_log, "CAN DeliveryList_Buffer \n");
    (void)fprintf(fp_log, "BUFFER_Use: %04zu  BUFFER_Max: %04d  Usage_Rate: %04u \n",
                  g_map_delivery_list.size(), CAN_DELIVERY_LIST_NUM, usage_rate);
    if (usage_rate >= CAN_USAGE_RATE_THRESHOLD) {
      (void)fprintf(fp_log, "Warning: Buffer utilization exceeds a threshold.\n");
    }
    (void)fprintf(fp_log, "CanID:      notifyId:\n");

    for (it = g_map_delivery_list.begin(); it != g_map_delivery_list.end(); it++) {
      (void)fprintf(fp_log, "%08x    %s\n",
                    it->first,
                    it->second.c_str());
    }
  }
}
// LCOV_EXCL_STOP

#ifdef CAN_DEBUG
EFrameworkunifiedStatus CANAllDeleteDeliveryEntry(HANDLE h_app) {
  g_map_delivery_list.clear();
  g_map__CWORD29__delivery_list.clear();

  return eFrameworkunifiedStatusOK;
}  // LCOV_EXCL_BR_LINE 10 last Line
#endif