summaryrefslogtreecommitdiffstats
path: root/peripheralservice/communication/server/src/threads/Thread_Common.cpp
blob: 718d948134604fff761b9c5feb5f3fa60df965c6 (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
/*
 * Copyright (c) 2019-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 <stdint.h>
#include <boost/atomic.hpp>
#include <pthread.h>

#include <native_service/frameworkunified_types.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/frameworkunified_timer.h>
#include <agl_thread.h>
#include <can_hal.h>
#include "communication_communicationlog.h"
#include "communication_cid.h"
#include "Thread_Common.h"

static std::map<std::string, HANDLE> g_map_sender;
static pthread_mutex_t g_sender_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
static boost::atomic<int32_t> availabilityCurrent;

void CommonInit(void) {
  pthread_mutex_lock(&g_sender_mutex);
  g_map_sender.clear();
  pthread_mutex_unlock(&g_sender_mutex);
}

HANDLE CommonFindSender(HANDLE h_app, std::string s) {
  HANDLE ret = NULL;
  std::string app(FrameworkunifiedGetAppName(h_app));
  std::string key = s + app;
  std::map<std::string, HANDLE>::iterator it = g_map_sender.find(key);

  pthread_mutex_lock(&g_sender_mutex);

  if (it != g_map_sender.end()) {
    ret = g_map_sender[key];
    goto cleanup;
  }

  ret = FrameworkunifiedMcOpenSender(h_app, s.c_str());
  if (ret) {
    g_map_sender[key] = ret;
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedMcOpenSender Error(to:%s)", s.c_str());
  }

cleanup:
  pthread_mutex_unlock(&g_sender_mutex);
  return ret;
}

void CommSetAvailabilityCurrent(int32_t current) {
  availabilityCurrent |= current;
}

void CommClrAvailabilityCurrent(int32_t current) {
  availabilityCurrent &= (~current);
}

BOOL CommGetAvailabilityCurrent(int32_t current) {
  return !(!(availabilityCurrent & current));
}

EFrameworkunifiedStatus CommonStartNotify(HANDLE h_app, PCSTR cmd) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  const char *result = "Unknown";
  const char *called = "Unknown";
  const BOOL avail = TRUE;
  UI_16 z = ZONE_ERR;

  e_status = FrameworkunifiedNPPublishNotification(h_app, cmd, &avail , sizeof(avail));
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedNPPublishNotification";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
  return e_status;
}

EFrameworkunifiedStatus CommonCanHalErrorNotify(HANDLE h_app) {
  char msg[CANHAL_ERROR_MESSAGE_LEN] = {0};
  EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;

  err = FrameworkunifiedGetMsgDataOfSize(h_app, &msg, sizeof(msg), eSMRRelease);
  if (err != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FrameworkunifiedGetMsgDataOfSize is failed, err=%d", err);
  }

  if (err == eFrameworkunifiedStatusInvldBufSize) {
    FrameworkunifiedClearMsgData(h_app);
    return eFrameworkunifiedStatusFail;
  } else if (err != eFrameworkunifiedStatusOK) {
    return eFrameworkunifiedStatusFail;
  }
  FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "can_hal error : %s", msg);
  return err;
}

EFrameworkunifiedStatus CommonThreadStart(HANDLE h_app, const FrameworkunifiedProtocolCallbackHandler *cb,
                      UI_32 count, PCSTR cmd, EFrameworkunifiedStatus (*open_func)(HANDLE)) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  const char *result = "Unknown";
  const char *called = "Unknown";
  UI_16 z = ZONE_ERR;

  e_status = FrameworkunifiedAttachCallbacksToDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, cb, count);
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedAttachCallbacksFromDispatcher";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s(%d)", called, result, e_status);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  e_status = open_func(h_app);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  e_status = FrameworkunifiedNPRegisterNotification(h_app, cmd,
                                       sizeof(EFrameworkunifiedStatus), eFrameworkunifiedStateVar);
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedNPRegisterNotification";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  return e_status;
}

EFrameworkunifiedStatus CommonThreadStop(HANDLE h_app, const PUI_32 cb,
                     UI_32 count, PCSTR cmd, EFrameworkunifiedStatus (*close_func)(HANDLE)) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  const BOOL avail = FALSE;
  const char *result = "Unknown";
  const char *called = "Unknown";
  UI_16 z = ZONE_ERR;

  e_status = FrameworkunifiedNPPublishNotification(h_app, cmd, &avail , sizeof(avail));
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedNPPublishNotification";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  e_status = FrameworkunifiedNPUnRegisterNotification(h_app, cmd);
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedNPUnRegisterNotification";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s(%s) %s(%d)", called, cmd, result, e_status);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  e_status = FrameworkunifiedDetachCallbacksFromDispatcher(h_app, FRAMEWORKUNIFIED_ANY_SOURCE, cb, count);
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "FrameworkunifiedDetachCallbacksFromDispatcher";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s(%d)", called, result, e_status);
  if (e_status != eFrameworkunifiedStatusOK)
    return e_status;

  e_status = close_func(h_app);
  if (e_status != eFrameworkunifiedStatusOK){
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: close_func() failed");
    return e_status;
  }

  return e_status;
}