RDK Documentation (Open Sourced RDK Components)
mocaSrvMgr.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include <iostream>
21 #include <stdint.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <stdio.h>
25 #include <glib.h>
26 #include <sys/types.h>
27 #include <ifaddrs.h>
28 #include "mocaSrvMgr.h"
29 #include "netsrvmgrIarm.h"
30 #include "NetworkMgrMain.h"
31 #include "NetworkMedium.h"
32 #include "netsrvmgrUtiles.h"
33 
34 
35 bool mocaLogEnable=true;
36 unsigned int mocaLogDuration=3600;
37 
38 
39 MocaNetworkMgr* MocaNetworkMgr::instance = NULL;
40 bool MocaNetworkMgr::instanceIsReady = false;
41 static RMH_Handle rmh = NULL;
42 
43 
44 MocaNetworkMgr::MocaNetworkMgr() {}
45 MocaNetworkMgr::~MocaNetworkMgr() { }
46 
47 MocaNetworkMgr* MocaNetworkMgr::getInstance()
48 {
49  if (instance == NULL)
50  {
51  instance = new MocaNetworkMgr();
52  rmh=RMH_Initialize(eventCallback, NULL); /* TODO: Need to find the correct location to call RMH_Destroy(rmh); */
53  instanceIsReady = true;
54  }
55  return instance;
56 }
57 
58 
59 int MocaNetworkMgr::Start()
60 {
61  bool retVal=false;
62  LOG_INFO("Enter");
63  IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG, _mocaEventHandler);
64  IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG_DURATION, _mocaEventHandler);
65  IARM_Bus_RegisterCall(IARM_BUS_NETWORK_MANAGER_MOCA_getTelemetryLogStatus, mocaTelemetryLogEnable);
66  IARM_Bus_RegisterCall(IARM_BUS_NETWORK_MANAGER_MOCA_getTelemetryLogDuration, mocaTelemetryLogDuration);
67  RMH_SetEventCallbacks(rmh, RMH_EVENT_LINK_STATUS_CHANGED | RMH_EVENT_MOCA_VERSION_CHANGED);
69  return 0;
70 }
71 
72 void *mocaTelemetryThrd(void* arg)
73 {
74  LOG_ENTRY_EXIT;
75  int ret = 0;
76 
77  MocaNetworkMgr::printMocaTelemetry();
78  while (true) {
79  if(mocaLogEnable)
80  {
81  MocaNetworkMgr::printMocaTelemetry();
82  sleep(mocaLogDuration);
83  }
84  else
85  {
86  sleep(300);
87  }
88  }
89 }
90 
92 {
93  pthread_t mocaTelemetryThread;
94  pthread_attr_t attr;
95  pthread_attr_init(&attr);
96  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
97  pthread_create(&mocaTelemetryThread, &attr, &mocaTelemetryThrd, NULL);
98 }
99 
100 
101 void MocaNetworkMgr::printMocaTelemetry()
102 {
103  LOG_ENTRY_EXIT;
104  RMH_LinkStatus status;
105  unsigned int ncNodeID;
106  uint8_t mac[6];
107  char macStr[32];
108  RMH_MoCAVersion mocaVersion;
109  unsigned int totalMoCANode;
110  unsigned int count=0;
111  unsigned int selfNodeId;
112  unsigned int i;
113  static char bFirst=1;
114  RMH_NodeList_Uint32_t nodeList;
115  GString *resString=g_string_new(NULL);
116 
117  if (RMH_Self_GetLinkStatus(rmh, &status) != RMH_SUCCESS) {
118  LOG_ERR("Failed calling RMH_Self_GetLinkStatus!");
119  }
120  else if(status == RMH_LINK_STATUS_UP)
121  {
122  LOG_INFO("TELEMETRY_MOCA_STATUS:UP");
123  if(bFirst)
124  {
125  LOG_INFO("TELEMETRY_MOCA_LINK_STATUS_CHANGED:%d",status);
126  bFirst=0;
127  if (RMH_Network_GetMoCAVersion(rmh,&mocaVersion) != RMH_SUCCESS)
128  {
129  LOG_ERR("Failed calling RMH_Network_GetMoCAVersion!");
130  }
131  else {
132  LOG_INFO("TELEMETRY_MOCA_VERSION_CHANGED:%s", RMH_MoCAVersionToString(mocaVersion));
133  }
134  }
135  if (RMH_Network_GetNCNodeId(rmh, &ncNodeID) != RMH_SUCCESS) {
136  LOG_ERR("Failed calling RMH_Network_GetNCNodeId!");
137  }
138  else {
139  LOG_INFO("TELEMETRY_MOCA_NC_NODEID:%d",ncNodeID);
140  }
141  if (RMH_Network_GetNCMac(rmh, &mac) != RMH_SUCCESS) {
142  LOG_ERR("Failed calling RMH_Network_GetNCMacString!");
143  }
144  else {
145  LOG_INFO("TELEMETRY_MOCA_NC_MAC:%s", RMH_MacToString(mac, macStr, sizeof(macStr)/sizeof(macStr[0])));
146  }
147 
148  if (RMH_Network_GetNumNodes(rmh, &totalMoCANode) != RMH_SUCCESS) {
149  LOG_ERR("Failed calling RMH_Network_GetNumNodes!");
150  }
151  else {
152  LOG_INFO("TELEMETRY_MOCA_TOTAL_NODE:%d",totalMoCANode);
153  }
154 
155  if (RMH_Network_GetNodeId(rmh, &selfNodeId) != RMH_SUCCESS) {
156  LOG_ERR("Failed calling RMH_Network_GetNodeId!");
157  } else if (RMH_Network_GetAssociatedIds(rmh, &nodeList) != RMH_SUCCESS) {
158  LOG_ERR("Failed calling RMH_Network_GetAssociatedIds!");
159  } else {
160  g_string_assign(resString,"");
161  for (i = 0; i < RMH_MAX_MOCA_NODES; i++) {
162  if (nodeList.nodePresent[i]) {
163  uint32_t phyRate;
164  if (RMH_RemoteNode_GetRxUnicastPhyRate(rmh, i, &phyRate) != RMH_SUCCESS) {
165  LOG_ERR("Failed calling RemoteNode_GetRxUnicastPhyRate!");
166  }
167  else {
168  LOG_INFO("TELEMETRY_MOCA_PHYRXRATE_%d_%d:%d", selfNodeId, i, phyRate);
169  g_string_append_printf(resString,"%d,",phyRate);
170  }
171  }
172  }
173  if (resString->len > 0) {
174  g_string_truncate(resString, resString->len-1); /*Remove last comma */
175  LOG_INFO("TELEMETRY_MOCA_PHY_RX_RATE:%s", resString->str);
176  g_string_assign(resString,"");
177  }
178 
179  for (i = 0; i < RMH_MAX_MOCA_NODES; i++) {
180  if (nodeList.nodePresent[i]) {
181  uint32_t phyRate;
182  if (RMH_RemoteNode_GetTxUnicastPhyRate(rmh, i, &phyRate) != RMH_SUCCESS) {
183  LOG_ERR("Failed calling RMH_RemoteNode_GetTxUnicastPhyRate!");
184  }
185  else {
186  LOG_INFO("TELEMETRY_MOCA_PHYTXRATE_%d_%d:%d", selfNodeId, i, phyRate);
187  g_string_append_printf(resString,"%d,",phyRate);
188  }
189  }
190  }
191  if (resString->len > 0) {
192  g_string_truncate(resString, resString->len-1); /*Remove last comma */
193  LOG_INFO("TELEMETRY_MOCA_PHY_TX_RATE:%s", resString->str);
194  g_string_assign(resString,"");
195  }
196 
197  for (i = 0; i < RMH_MAX_MOCA_NODES; i++) {
198  if (nodeList.nodePresent[i]) {
199  float power;
200  if (RMH_RemoteNode_GetRxUnicastPower(rmh, i, &power) != RMH_SUCCESS) {
201  LOG_ERR("Failed calling RemoteNode_GetRxUnicastPower!");
202  }
203  else {
204  LOG_INFO("TELEMETRY_MOCA_RXPOWER_%d_%d:%2.02f", selfNodeId, i, power);
205  g_string_append_printf(resString,"%2.02f,",power);
206  }
207  }
208  }
209  if (resString->len > 0) {
210  g_string_truncate(resString, resString->len-1); /*Remove last comma */
211  LOG_INFO("TELEMETRY_MOCA_RX_POWER:%s", resString->str);
212  g_string_assign(resString,"");
213  }
214 
215  g_string_assign(resString,"");
216  for (i = 0; i < RMH_MAX_MOCA_NODES; i++) {
217  if (nodeList.nodePresent[i]) {
218  uint32_t powerReduction;
219  if (RMH_RemoteNode_GetTxPowerReduction(rmh, i, &powerReduction) != RMH_SUCCESS) {
220  LOG_ERR("Failed calling RMH_RemoteNode_GetTxPowerReduction!");
221  }
222  else {
223  LOG_INFO("TELEMETRY_MOCA_TXPOWERREDUCTION_%d_%d:%d", selfNodeId, i, powerReduction);
224  g_string_append_printf(resString,"%d,",powerReduction);
225  }
226  }
227  }
228  if (resString->len > 0) {
229  g_string_truncate(resString, resString->len-1); /*Remove last comma */
230  LOG_INFO("TELEMETRY_MOCA_TX_POWER_REDUCTION:%s", resString->str);
231  g_string_assign(resString,"");
232  }
233 
234  }
235  if (selfNodeId == ncNodeID )
236  {
237  LOG_INFO("TELEMETRY_MOCA_IS_CURRENT_NODE_NC:1");
238  }
239  else
240  {
241  LOG_INFO("TELEMETRY_MOCA_IS_CURRENT_NODE_NC:0");
242  }
243 
244  }
245  else
246  {
247  LOG_INFO("TELEMETRY_MOCA_STATUS:DOWN");
248 
249  }
250  g_string_free(resString,TRUE);
251 }
252 
253 static void _mocaEventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len)
254 {
255  LOG_ENTRY_EXIT;
256  if (strcmp(owner, IARM_BUS_NM_SRV_MGR_NAME) == 0) {
257  switch (eventId) {
258  case IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG:
259  {
260  bool *param = (bool *)data;
261  mocaLogEnable=*param;
262  LOG_INFO("got event IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG %d", mocaLogEnable);
263  }
264  break;
265  case IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG_DURATION:
266  {
267  unsigned int *param = (unsigned int *)data;
268  mocaLogDuration=*param;
269  LOG_INFO("got event IARM_BUS_NETWORK_MANAGER_MOCA_TELEMETRY_LOG_DURATION %d", mocaLogDuration);
270  }
271  break;
272  default:
273  break;
274  }
275  }
276 }
277 
278 IARM_Result_t MocaNetworkMgr::mocaTelemetryLogEnable(void *arg)
279 {
280  LOG_ENTRY_EXIT;
281  IARM_Result_t ret = IARM_RESULT_SUCCESS;
282  bool *param = (bool *)arg;
283  *param=mocaLogEnable;
284  LOG_INFO("get event to get status of telemetry log enable %d", mocaLogEnable);
285  return ret;
286 }
287 
288 IARM_Result_t MocaNetworkMgr::mocaTelemetryLogDuration(void *arg)
289 {
290  LOG_ENTRY_EXIT;
291  IARM_Result_t ret = IARM_RESULT_SUCCESS;
292  unsigned int *param = (unsigned int *)arg;
293  *param=mocaLogDuration;
294  LOG_INFO("get venent to get the moca log duration set %d", mocaLogDuration);
295  return ret;
296 }
297 
298 static void eventCallback(const enum RMH_Event event, const struct RMH_EventData *eventData, void* userContext) {
299  switch(event) {
300  case RMH_EVENT_LINK_STATUS_CHANGED:
301  LOG_INFO("MoCA Link status changed to %d", eventData->RMH_EVENT_LINK_STATUS_CHANGED.status );
302  LOG_INFO("TELEMETRY_MOCA_LINK_STATUS_CHANGED:%d", eventData->RMH_EVENT_LINK_STATUS_CHANGED.status);
303  break;
304  case RMH_EVENT_MOCA_VERSION_CHANGED:
305  LOG_INFO("MoCA version changed to %s", RMH_MoCAVersionToString(eventData->RMH_EVENT_MOCA_VERSION_CHANGED.version));
306  LOG_INFO("TELEMETRY_MOCA_VERSION_CHANGED:%s", RMH_MoCAVersionToString(eventData->RMH_EVENT_MOCA_VERSION_CHANGED.version));
307  break;
308  default:
309  LOG_ERR("MoCA Event Not Supported");
310  break;
311  }
312 }
RMH_Network_GetNCMac
RMH_Result RMH_Network_GetNCMac(const RMH_Handle handle, RMH_MacAddress_t *response)
Return the MAC address of the network coordinator.
startMocaTelemetry
void startMocaTelemetry()
Initializes a thread to retrieve and send MoCA related telemetry data.
Definition: mocaSrvMgr.cpp:91
netsrvmgrIarm.h
The header file provides components netSrvMgrIarm information APIs.
RMH_Self_GetLinkStatus
RMH_Result RMH_Self_GetLinkStatus(const RMH_Handle handle, RMH_LinkStatus *status)
Current operational status of the MoCA interface [mocaIfStatus].
mocaTelemetryThread
void * mocaTelemetryThread(void *arg)
This Thread function prints the statistics information on MOCA interface if the MOCA link is up.
RMH_NodeList_Uint32_t
Definition: rmh_type.h:195
RMH_MoCAVersionToString
const char *const RMH_MoCAVersionToString(const RMH_MoCAVersion value)
Convert RMH_MoCAVersion to a string.
Definition: librmh_api_no_wrap.c:122
RMH_Network_GetNodeId
RMH_Result RMH_Network_GetNodeId(const RMH_Handle handle, uint32_t *response)
Return the node ID of this device.
RMH_Network_GetNumNodes
RMH_Result RMH_Network_GetNumNodes(const RMH_Handle handle, uint32_t *response)
Return the number of MoCA nodes in the network. [mocaIfNumNodes].
IARM_Bus_RegisterEventHandler
IARM_Result_t IARM_Bus_RegisterEventHandler(const char *ownerName, IARM_EventId_t eventId, IARM_EventHandler_t handler)
This API register to listen to event and provide the callback function for event notification....
Definition: iarmMgrMocks.cpp:43
IARM_Bus_RegisterCall
IARM_Result_t IARM_Bus_RegisterCall(const char *methodName, IARM_BusCall_t handler)
This API is used to register an RPC method that can be invoked by other applications.
netsrvmgrUtiles.h
The header file provides components netSrvMgrUtiles information APIs.
RMH
Definition: librmh.h:57
RMH_EventData
Definition: rmh_type.h:157
RMH_RemoteNode_GetRxUnicastPhyRate
RMH_Result RMH_RemoteNode_GetRxUnicastPhyRate(const RMH_Handle handle, const uint32_t nodeId, uint32_t *response)
Return the PHY rate at which unicast packets are received from nodeId of the self node.
RMH_Network_GetAssociatedIds
RMH_Result RMH_Network_GetAssociatedIds(const RMH_Handle handle, RMH_NodeList_Uint32_t *response)
Return a list of the associated ID for every node on the network.
RMH_MacToString
const char *const RMH_MacToString(const RMH_MacAddress_t value, char *responseBuf, const size_t responseBufSize)
Returns the provided MAC address in value as a string.
Definition: librmh_api_no_wrap.c:174
RMH_Initialize
RMH_Handle RMH_Initialize(const RMH_EventCallback eventCB, void *userContext)
Initialize the RMH library and return a handle to the instance.
Definition: librmh_api_no_wrap.c:23
RMH_Network_GetNCNodeId
RMH_Result RMH_Network_GetNCNodeId(const RMH_Handle handle, uint32_t *response)
Return the node ID of the network coordinator. [mocaIfNC].
MocaNetworkMgr
Definition: mocaSrvMgr.h:24
RMH_SetEventCallbacks
RMH_Result RMH_SetEventCallbacks(RMH_Handle handle, const uint32_t value)
Set the list of callbacks you wish to receive.
RMH_RemoteNode_GetTxPowerReduction
RMH_Result RMH_RemoteNode_GetTxPowerReduction(const RMH_Handle handle, const uint32_t nodeId, uint32_t *response)
The transmit power control back-off used for transmissions from the specified nodeId.
RMH_RemoteNode_GetTxUnicastPhyRate
RMH_Result RMH_RemoteNode_GetTxUnicastPhyRate(const RMH_Handle handle, const uint32_t nodeId, uint32_t *response)
Return the PHY rate at which unicast packets are transmitted from nodeId to the self node.
LOG_INFO
#define LOG_INFO(AAMP_JS_OBJECT, FORMAT,...)
Definition: jsutils.h:39
RMH_Network_GetMoCAVersion
RMH_Result RMH_Network_GetMoCAVersion(const RMH_Handle handle, RMH_MoCAVersion *response)
Return the version of MoCA under which the network is operating. [mocaIfNetworkVersion].
RMH_RemoteNode_GetRxUnicastPower
RMH_Result RMH_RemoteNode_GetRxUnicastPower(const RMH_Handle handle, const uint32_t nodeId, float *response)
Return the power level at which unicast packets are received from nodeId from the self node.
TRUE
#define TRUE
Defines for TRUE/FALSE/ENABLE flags.
Definition: wifi_common_hal.h:199