2023-01-14 01:09:03 +01:00
|
|
|
#include <stdio.h>
|
2023-01-16 22:26:15 +01:00
|
|
|
#include <stdlib.h>
|
2023-01-17 07:51:41 +01:00
|
|
|
#include <string.h>
|
2023-01-14 01:09:03 +01:00
|
|
|
|
2023-01-17 04:33:09 +01:00
|
|
|
#include "../libipc.h"
|
|
|
|
|
2023-01-16 23:58:30 +01:00
|
|
|
#define SERVICE "pong"
|
|
|
|
#define SERVICE_LEN 4
|
|
|
|
|
2023-01-17 05:47:14 +01:00
|
|
|
int direct_write_then_read(void);
|
|
|
|
int wait_event(void);
|
2023-01-17 01:27:55 +01:00
|
|
|
|
2023-01-14 01:09:03 +01:00
|
|
|
int main(void) {
|
2023-01-17 01:27:55 +01:00
|
|
|
direct_write_then_read();
|
|
|
|
wait_event();
|
|
|
|
}
|
|
|
|
|
|
|
|
int direct_write_then_read(void) {
|
2023-01-14 01:09:03 +01:00
|
|
|
int ret = 0;
|
2023-01-16 23:58:30 +01:00
|
|
|
int servicefd = 0;
|
2023-01-14 01:09:03 +01:00
|
|
|
|
|
|
|
printf ("Init context.\n");
|
|
|
|
void *ctx = NULL;
|
|
|
|
ret = ipc_context_init (&ctx);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot init context.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Connect to a 'pong' service.\n");
|
2023-01-16 23:58:30 +01:00
|
|
|
ret = ipc_connect_service (ctx, &servicefd, SERVICE, SERVICE_LEN);
|
2023-01-14 01:09:03 +01:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot connect to a service.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-14 01:09:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-16 23:58:30 +01:00
|
|
|
printf ("Let's send a message.\n");
|
|
|
|
ret = ipc_write (ctx, servicefd, "hello, plz bounce me", 21);
|
2023-01-14 01:09:03 +01:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot write to the service.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-14 01:09:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-17 00:16:58 +01:00
|
|
|
char message[10000];
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t size = 10000;
|
2023-01-17 01:27:55 +01:00
|
|
|
|
2023-01-17 00:16:58 +01:00
|
|
|
ret = ipc_read_fd (ctx, servicefd, message, &size);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot read from the service fd: %d.\n", ret);
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-17 00:16:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2023-01-14 01:09:03 +01:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
printf ("No message returned.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-14 01:09:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
message[size] = '\0';
|
|
|
|
printf ("Response: %s.\n", message);
|
2023-01-16 22:26:15 +01:00
|
|
|
|
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
|
|
|
|
free(ctx);
|
2023-01-14 01:09:03 +01:00
|
|
|
|
2023-01-16 22:26:15 +01:00
|
|
|
printf ("Context freed.\n");
|
2023-01-14 01:09:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2023-01-17 01:27:55 +01:00
|
|
|
|
|
|
|
int wait_event(void) {
|
|
|
|
int ret = 0;
|
|
|
|
int servicefd = 0;
|
|
|
|
char message[10000];
|
2023-01-17 07:51:41 +01:00
|
|
|
memset (message, 0, 1000);
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t size = 10000;
|
2023-01-17 01:27:55 +01:00
|
|
|
char event_type;
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t index = 0;
|
2023-01-17 01:27:55 +01:00
|
|
|
int originfd = 0;
|
|
|
|
void *ctx = NULL;
|
|
|
|
|
|
|
|
printf ("Init context.\n");
|
|
|
|
ret = ipc_context_init (&ctx);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot init context.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-17 01:27:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Connect to a 'pong' service.\n");
|
|
|
|
ret = ipc_connect_service (ctx, &servicefd, SERVICE, SERVICE_LEN);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot connect to a service.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-17 01:27:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Let's schedule a message.\n");
|
|
|
|
ret = ipc_schedule (ctx, servicefd, "hello, plz bounce me", 21);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot schedule a message.\n");
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
free(ctx);
|
2023-01-17 01:27:55 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:33:09 +01:00
|
|
|
printf ("Let's set the timer to one second.\n");
|
|
|
|
ipc_context_timer (ctx, 1000);
|
|
|
|
|
2023-01-17 01:27:55 +01:00
|
|
|
printf ("Let's loop over events.\n");
|
|
|
|
char should_continue = 1;
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t count = 0;
|
2023-01-17 01:27:55 +01:00
|
|
|
while(should_continue) {
|
|
|
|
size = 10000;
|
|
|
|
ret = ipc_wait_event (ctx, &event_type, &index, &originfd, message, &size);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Error while waiting for an event.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-17 05:47:14 +01:00
|
|
|
printf ("EVENT %lu\t", count++);
|
2023-01-17 01:27:55 +01:00
|
|
|
|
|
|
|
switch ((enum event_types) event_type) {
|
|
|
|
case ERROR: {
|
|
|
|
printf ("Error.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case EXTERNAL: {
|
|
|
|
printf ("External (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case SWITCH_RX: {
|
|
|
|
printf ("Switch RX (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case SWITCH_TX: {
|
|
|
|
printf ("Switch TX (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case CONNECTION: {
|
|
|
|
printf ("Connection (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case DISCONNECTION: {
|
|
|
|
printf ("Disconnection (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case TIMER: {
|
|
|
|
printf ("TIMER.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TX: {
|
|
|
|
printf ("A message has been sent.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE: {
|
|
|
|
if (size == 0) {
|
|
|
|
printf ("No message returned.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
message[size] = '\0';
|
2023-01-17 07:51:41 +01:00
|
|
|
printf ("Response (size %lu): %s.\n", size, message);
|
2023-01-17 01:27:55 +01:00
|
|
|
// We received the response, quitting.
|
|
|
|
should_continue = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
|
|
|
|
printf ("Context freed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|