rdkcentral / ut-core

Unit Test - Core Framework
Apache License 2.0
4 stars 0 forks source link

UT_Control Plane - Control message system for running component #20

Closed Ulrond closed 2 months ago

Ulrond commented 7 months ago

This control plane will support receiving messages via JSON/ YAML RPC on a websocket.

The module will open a port and wait for control messages.

Any client module is linked against ut-core will register interest in received message type from the control plane.

Full design of the implementation is yet to be defined but an idea on the server side you would register interest in messages


#define UT_CONTROL_PLANE_MAX_KEY_SIZE (256)
#define UT_CONTROL_PLANE_MAX_KEY_SIZE (64)

typedef struct
{
  char key[UT_CONTROL_PLANE_MAX_KEY_SIZE];
  ut_control_callback_t pCallback;
}CallbackEntry_t;

static callbackEntry_t callbackList[UT_CONTROL_PLANE_MAX_CALLBACK_ENTRIES);
static uint32_t lastFreeCallbackSlot=0; /* Must always be < UT_CONTROL_PLANE_MAX_CALLBACK_ENTRIES */

/* Callback function that will be triggered */
void (*ut_control_callback_t)( char *key, ut_kvp_instance_t *instance );

/* Init the control plane and create instance */
ut_controlPlane_instance_t *UT_ControlPlane_Init( uint32_t monitorPort );

/* pInstance, will be freed on return from the callback */
UT_ControlPlane_RegisterCallbackOnMessage( ut_controlPlane_instance_t *pInstance, char* key, ut_control_callback_t  callbackFunction);

/* Exit the controlPlane instance and release */
void UT_ControlPlane_Exit( ut_controlPlane_instance_t *pInstance );
...

On the client side, you would send message e.g.

hdmicec:
  command: hotplug
  port:10
  on: true

rmfAudio:
 a:1
 b:2 
sequenceDiagram
      actor Test_Control
      participant Control_Plane
      participant vDevice_Control_Plane
      Test_Control -->> Control_Plane: Request Behaviour
      Control_Plane -->> vDevice_Control_Plane: Platform control to vDevice

Example Socket connection using https://libwebsockets.org/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libwebsockets.h>

static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {

    switch (reason) {
        case LWS_CALLBACK_CLIENT_ESTABLISHED:
            printf("Connection established\n");
            break;

        case LWS_CALLBACK_CLIENT_RECEIVE:
            // Message received - assuming JSON format like {"symbol": "AAPL", "price": 155.23}
            printf("Received message: %.*s\n", (int)len, (char *)in);  
            // TODO: Parse the JSON for symbol and price
            break;

        // Other events as needed ... (connection closed, etc.)

        default:
            break;
    }

    return 0;
}

static struct lws_protocols protocols[] = {
    { "http-only", callback_http, 0, 0 },
    { NULL, NULL, 0, 0 } /* terminator */
};

int main(void) {
    struct lws_context_creation_info info;
    struct lws_context *context;
    const char *server_address = "YOUR_WEBSOCKET_SERVER_ADDRESS";
    int port = 80; // Or whatever port is used by your provider

    memset(&info, 0, sizeof info);
    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;

    context = lws_create_context(&info);
    if (!context) {
        fprintf(stderr, "Failed to create libwebsockets context\n");
        return 1;
    }

    // Connect to the server
    struct lws *wsi = lws_client_connect(context, server_address, port, 0, "/", server_address, NULL, NULL, -1);
    if (!wsi) {
        fprintf(stderr, "Failed to connect to server\n");
        lws_context_destroy(context);
        return 1;
    }

    // Event loop
    while (1) {
        lws_service(context, 1000); // Timeout of 1000 ms
    }

    lws_context_destroy(context);
    return 0;
}
Ulrond commented 3 months ago
kanjoe24 commented 3 months ago

Tasks executed for this issue:

Pending Activity:

Ulrond commented 3 months ago
kanjoe24 commented 3 months ago

Following activities are completed:

Pending activities:

kanjoe24 commented 2 months ago