RDK Documentation (Open Sourced RDK Components)
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
rmh_monitor.h
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 #ifndef RMH_APP_H
20 #define RMH_APP_H
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/queue.h>
26 #include "rdk_moca_hal.h"
27 
28 /**
29  * A simple semaphore based on pthread
30  */
31 typedef struct RMHMonitor_Semaphore {
32  pthread_mutex_t lock;
33  pthread_cond_t cond;
34  bool signal;
36 
37 /**
38  * Contains all necessary information to describe a callback from RMH. This allows
39  * us to handle the event on a seperate thread then where the callback occurred.
40  */
41 typedef struct RMHMonitor_CallbackEvent {
42  RMH_Event event; /* The event that occurred */
43  RMH_EventData eventData; /* Whatever event data was provided with the event. Because this event will be handled
44  on a different thread, if eventData is ever a pointer we will need to copy. */
45  struct timeval eventTime; /* The time the event occurred */
46  TAILQ_ENTRY(RMHMonitor_CallbackEvent) entries; /* For queuing */
48 
49 
50 /**
51  * Stores information about a given node. We store this interally it so if a node leaves the network we still know something about it.
52  */
53 typedef struct RMHMonitor_NodeInfo {
54  bool joined; /* Set to true if this node has actively joined the network */
55  bool preferredNC; /* Set to true if this node is a preferred NC */
56  RMH_MacAddress_t mac; /* The MAC address of the device */
57  RMH_MoCAVersion mocaVersion; /* The highest version of MoCA supported by this node */
59 
60 /**
61  * The current snapshot status of the network. We store this internally so we can check callbacks to ensure things have acutally changed
62  * and we don't print duplicate messages.
63  */
64 typedef struct RMHMonitor_NetworkStatus {
65  RMH_MoCAVersion networkMoCAVer; /* The version of MoCA being used by the Network */
66  bool networkMoCAVerValid; /* Set to true if the value of 'networkMoCAVer' is valid */
67  uint32_t ncNodeId; /* The node ID of the NC */
68  bool ncNodeIdValid; /* Set to true if the value of 'ncNodeId' is valid */
69  uint32_t selfNodeId; /* The node ID of the local device */
70  RMHMonitor_NodeInfo nodes[RMH_MAX_MOCA_NODES]; /* Node info for all remote devices */
72 
73 
74 /**
75  * This is the main sturcture of the applicaiton and contains everything needing to be shared between functions.
76  */
77 typedef struct RMHMonitor {
78  RMH_Handle rmh; /* The handle to RMH for all MoCA calls */
79 
80  pthread_t eventThread; /* The thread which handles the MoCA events */
81  bool eventThreadRunning; /* Must remain set to true to continue monitoring. If this goes to false the thread will exit. */
82  TAILQ_HEAD(tailhead, RMHMonitor_CallbackEvent) eventQueue;
83  /* The queue where events are stored while they are moved to the event thread */
84  pthread_mutex_t eventQueueProtect; /* A mutex to ensure safe enqueue/dequeue of events */
85  RMHMonitor_hSemaphore eventNotify; /* A semaphore to notify the event thread there is work to be done */
86  struct timeval lastPrint; /* The time the last message was print to the log */
87 
88  RMH_LinkStatus linkStatus; /* The current state of the MoCA link for this device */
89  bool linkStatusValid; /* Set to true if the value of 'linkStatus' is valid */
90  RMHMonitor_NetworkStatus netStatus; /* The current state of the MoCA network */
91  uint32_t reconnectSeconds; /* Number of seconds to wait between attempts to reconnect to MoCA */
92 
93  RMH_LogLevel apiLogLevel; /* The logging level to print from the app and RMH */
94  RMH_LogLevel driverLogLevel; /* The logging level to print from the MoCA driver itself */
95  const char* appPrefix; /* A fixed string to prepend to every line */
96  bool userSetTimestamps; /* Set to true to note the user explicitly set printTimestamp */
97  bool printTimestamp; /* Set to true to prefix each output line with a timestamp */
98  bool serviceMode; /* Set to true if the app is being started as a service. This will allow us to do things like systemd notify */
99  const char *out_file_name; /* The name of the file to write output to */
100  FILE* out_file; /* The handle of the file to write output to */
101 } RMHMonitor;
102 
103 
104 /* Logging functions to be used throughout the application */
105 #define RMH_PrintErr(fmt, ...) RMH_Print(app, NULL, RMH_LOG_ERROR, "ERROR: ", fmt, ##__VA_ARGS__);
106 #define RMH_PrintWrn(fmt, ...) RMH_Print(app, NULL, RMH_LOG_ERROR, "WARNING: ", fmt, ##__VA_ARGS__);
107 #define RMH_PrintMsg(fmt, ...) RMH_Print(app, NULL, RMH_LOG_MESSAGE, "", fmt, ##__VA_ARGS__);
108 #define RMH_PrintMsgT(time, fmt, ...) RMH_Print(app, time, RMH_LOG_MESSAGE, "", fmt, ##__VA_ARGS__);
109 #define RMH_PrintDbg(fmt, ...) RMH_Print(app, NULL, RMH_LOG_DEBUG, "", fmt, ##__VA_ARGS__);
110 
111 /* Low level logging fuctions which are not intended to be called directly */
112 void RMH_Print_Raw(RMHMonitor *app, struct timeval *time, const char*fmt, ...);
113 #define RMH_Print(app, time, level, logPrefix, fmt, ...) { \
114  if (app && (app->apiLogLevel & level) == level) { \
115  struct timeval now; \
116  gettimeofday(&now, NULL); \
117  app->lastPrint=now; \
118  if (time != NULL) { \
119  RMH_Print_Raw(app, time, fmt, ##__VA_ARGS__); \
120  } \
121  else { \
122  RMH_Print_Raw(app, &now, fmt, ##__VA_ARGS__); \
123  } \
124  } \
125 }
126 
127 
128 
129 RMHMonitor_hSemaphore RMHMonitor_Semaphore_Create();
130 void RMHMonitor_Semaphore_Destroy(RMHMonitor_hSemaphore eventHandle);
131 RMH_Result RMHMonitor_Semaphore_Reset(RMHMonitor_hSemaphore eventHandle);
132 RMH_Result RMHMonitor_Semaphore_Signal(RMHMonitor_hSemaphore eventHandle);
133 RMH_Result RMHMonitor_Semaphore_WaitTimeout(RMHMonitor_hSemaphore eventHandle, const int timeoutMsec);
134 void RMHMonitor_Queue_Dequeue(RMHMonitor *app);
135 void RMHMonitor_Queue_Enqueue(RMHMonitor *app, const enum RMH_Event event, const struct RMH_EventData *eventData);
136 
137 
138 void RMHMonitor_Event_Thread(void * context);
139 void RMHMonitor_RMHCallback(const enum RMH_Event event, const struct RMH_EventData *eventData, void* userContext);
140 
141 #endif
RMHMonitor_NetworkStatus
Definition: rmh_monitor.h:64
RMHMonitor
Definition: rmh_monitor.h:77
RMH
Definition: librmh.h:57
RMH_EventData
Definition: rmh_type.h:157
RMHMonitor_NodeInfo
Definition: rmh_monitor.h:53
RMHMonitor_CallbackEvent
Definition: rmh_monitor.h:41
RMHMonitor_Semaphore
Definition: rmh_monitor.h:31