範例程式顯示的訊息必須使用系統指令
dmesg 觀看.
外部程式部分
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include "mcm_lib/mcm_lheader/mcm_type.h"
#include "mcm_lib/mcm_lheader/mcm_size.h"
#include "mcm_lib/mcm_lheader/mcm_connect.h"
#include "mcm_lib/mcm_lheader/mcm_return.h"
#include "mcm_lib/mcm_lheader/mcm_data_exinfo_auto.h"
#include "mcm_lib/mcm_lklib/mcm_lklib_api.h"
#define DMSG(msg_fmt, msgs...) \
printk(KERN_INFO "%s(%04u): " msg_fmt "\n", \
strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__, __LINE__, ##msgs)
static int __init main_init(
void)
{
char *path1;
struct mcm_lklib_lib_t self_lklib;
char req_data1[128], *rep_data1;
MCM_DTYPE_USIZE_TD req_len1, rep_len1;
int req_data2[4], *rep_data2;
MCM_DTYPE_USIZE_TD req_len2, rep_len2, idx, count;
self_lklib.socket_path = "@mintcm";
self_lklib.call_from = MCM_CFROM_KERNEL;
self_lklib.session_permission = MCM_SPERMISSION_RO;
self_lklib.session_stack_size = 0;
if(mcm_lklib_init(&self_lklib) < MCM_RCODE_PASS)
{
DMSG("call mcm_lklib_init() fail");
goto FREE_01;
}
// 測試-01 : 字串類型資料.
path1 = "mcm_module_kernel_data_test_01";
DMSG("[run] %s", path1);
snprintf(req_data1, sizeof(req_data1), "kernel-data-%u", 123456789);
req_len1 = strlen(req_data1) + 1;
DMSG("send [" MCM_DTYPE_USIZE_PF "][%s]", req_len1, req_data1);
if(mcm_lklib_run(&self_lklib, path1, &req_data1, req_len1, (void **) &rep_data1, &rep_len1)
< MCM_RCODE_PASS)
{
DMSG("call mcm_lklib_run(%s) fail", path1);
goto FREE_02;
}
DMSG("recv [" MCM_DTYPE_USIZE_PF "][%s]", rep_len1, rep_data1);
kfree(rep_data1);
// 測試-02 : 字節類型資料.
path1 = "mcm_module_kernel_data_test_02";
DMSG("[run] %s", path1);
count = sizeof(req_data2) / sizeof(int);
for(idx = 0; idx < count; idx++)
req_data2[idx] = (idx + 1) * 10;
req_len2 = sizeof(req_data2);
DMSG("send [" MCM_DTYPE_USIZE_PF "] (" MCM_DTYPE_USIZE_PF ") -", req_len2, count);
for(idx = 0; idx < count; idx++)
{
DMSG("%d", req_data2[idx]);
}
if(mcm_lklib_run(&self_lklib, path1, &req_data2, req_len2, (void **) &rep_data2, &rep_len2)
< MCM_RCODE_PASS)
{
DMSG("call mcm_lklib_run(%s) fail", path1);
goto FREE_02;
}
count = rep_len2 / sizeof(int);
DMSG("recv [" MCM_DTYPE_USIZE_PF "] (" MCM_DTYPE_USIZE_PF ") -", rep_len2, count);
for(idx = 0; idx < count; idx++)
{
DMSG("%d", rep_data2[idx]);
}
kfree(rep_data2);
FREE_02:
mcm_lklib_exit(&self_lklib);
FREE_01:
return 0;
}
static void __exit main_exit(
void)
{
return;
}
module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");
內部模組部分
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mcm_lib/mcm_lheader/mcm_type.h"
#include "mcm_lib/mcm_lheader/mcm_size.h"
#include "mcm_lib/mcm_lheader/mcm_control.h"
#include "mcm_lib/mcm_lheader/mcm_connect.h"
#include "mcm_lib/mcm_lheader/mcm_return.h"
#include "mcm_lib/mcm_lheader/mcm_debug.h"
#include "mcm_lib/mcm_lheader/mcm_data_exinfo_auto.h"
#include "../mcm_service_handle_define.h"
#include "../mcm_config_handle_extern.h"
#define DMSG(msg_fmt, msgs...) printf("%s(%04u): " msg_fmt "\n", __FILE__, __LINE__, ##msgs)
int mcm_module_kernel_data_test_01(
struct mcm_service_session_t *this_session)
{
int fret = MCM_RCODE_MODULE_INTERNAL_ERROR;
MCM_DTYPE_USIZE_TD tmp_len;
char *tmp_buf;
DMSG("string data test :");
srand(time(NULL));
// 外部程式傳來的資料.
DMSG("recv [" MCM_DTYPE_USIZE_PF "][%s]",
this_session->req_data_len, (char *) this_session->req_data_con);
// 內部模組要回傳的資料.
tmp_len = 256;
tmp_buf = (char *) malloc(tmp_len);
if(tmp_buf == NULL)
{
DMSG("call malloc() fail [%s]", strerror(errno));
goto FREE_01;
}
snprintf(tmp_buf, tmp_len, "internal data %u", rand());
tmp_len = strlen(tmp_buf) + 1;
DMSG("send [" MCM_DTYPE_USIZE_PF "][%s]", tmp_len, tmp_buf);
this_session->rep_data_buf = tmp_buf;
this_session->rep_data_len = tmp_len;
fret = MCM_RCODE_PASS;
FREE_01:
return fret;
}
int mcm_module_kernel_data_test_02(
struct mcm_service_session_t *this_session)
{
int fret = MCM_RCODE_MODULE_INTERNAL_ERROR;
MCM_DTYPE_USIZE_TD tmp_len, idx, count;
int *tmp_buf;
DMSG("bytes data test :");
srand(time(NULL));
// 外部程式傳來的資料.
tmp_buf = (int *) this_session->req_data_con;
count = this_session->req_data_len / sizeof(int);
DMSG("recv [" MCM_DTYPE_USIZE_PF "] (" MCM_DTYPE_USIZE_PF ") -",
this_session->req_data_len, count);
for(idx = 0; idx < count; idx++)
{
DMSG("%d", tmp_buf[idx]);
}
// 內部模組要回傳的資料.
count = 6;
tmp_buf = calloc(count, sizeof(int));
if(tmp_buf == NULL)
{
DMSG("call calloc() fail [%s]", strerror(errno));
goto FREE_01;
}
tmp_len = sizeof(int) * count;
for(idx = 0; idx < count; idx++)
tmp_buf[idx] = rand() % 10;
DMSG("send [" MCM_DTYPE_USIZE_PF "] (" MCM_DTYPE_USIZE_PF ") -", tmp_len, count);
for(idx = 0; idx < count; idx++)
{
DMSG("%d", tmp_buf[idx]);
}
this_session->rep_data_buf = tmp_buf;
this_session->rep_data_len = tmp_len;
fret = MCM_RCODE_PASS;
FREE_01:
return fret;
}