Skip to content

Commit a62e886

Browse files
committed
cm: New header file for private CM types/func declaration
This new file would hold types/function/macro definitions which are private to the CM library and should not be exposed to the applications. CM library is spread out in multiple files and there are cases when a function/type which is defined in one file need to be exposed to another file such that its not exposed to the applications. Also includes: * `CM_RETURN_ERROR` macro definition and definition of the `cm_err_num` global variable were moved to `kcmlib.h` since they are a private items.
1 parent 5baa1a9 commit a62e886

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

include/cm/err.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,9 @@ typedef enum CMErrors {
2424
CM_ERR_EVENT_HANDLER_ALREADY_REGISTERED = 101,
2525
} CMErrors;
2626

27-
extern uint32_t cm_error_num__;
28-
29-
static inline uint32_t cm_get_lib_error()
30-
{
31-
return cm_error_num__;
32-
}
27+
uint32_t cm_get_lib_error();
3328

3429
static inline uint32_t cm_get_os_error()
3530
{
3631
return (uint32_t)syscall (OSIF_SYSCALL_GET_OS_ERROR, 0, 0, 0, 0, 0);
3732
}
38-
39-
/* Can be used to store an error code and return from a function */
40-
#define CM_RETURN_ERROR__(errno, rval) \
41-
do { \
42-
CM_DBG_ERROR ("Error %x.", errno); \
43-
cm_error_num__ = errno; \
44-
return rval; \
45-
} while (0)

include/kcmlib.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* ---------------------------------------------------------------------------
3+
* Megha Operating System V2 - App library Kernel Header - Private type/function declarations
4+
* ---------------------------------------------------------------------------
5+
* Types and function declarations defined here are private to the CM library and should not be
6+
* exposed to the applications.
7+
*/
8+
9+
#include <intrusive_list.h>
10+
#include <memloc.h>
11+
12+
#pragma once
13+
14+
extern uint32_t cm_error_num;
15+
16+
/* Can be used to store an error code and return from a function */
17+
#define CM_RETURN_ERROR(errno, rval) \
18+
do { \
19+
CM_DBG_ERROR ("Error %x.", errno); \
20+
cm_error_num = errno; \
21+
return rval; \
22+
} while (0)

src/cm/cm.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
#include <cm/cm.h>
99
#include <cm/syscall.h>
1010
#include <cm/debug.h>
11+
#include <kcmlib.h>
1112

1213
/* Variable to store Library error*/
13-
uint32_t cm_error_num__;
14+
uint32_t cm_error_num;
1415

1516
#define cm_MICRODEC_TO_TICK_COUNT(us) ((us) / cm_get_tick_period_us())
1617

18+
/***************************************************************************************************
19+
* Halts thread for 'ms' miliseconds
20+
*
21+
* @return Nothing
22+
**************************************************************************************************/
1723
void cm_delay (UINT ms)
1824
{
1925
UINT us = ms * 1000;
@@ -25,3 +31,13 @@ void cm_delay (UINT ms)
2531
cm_process_handle_events();
2632
}
2733
}
34+
35+
/***************************************************************************************************
36+
* Return the last library error
37+
*
38+
* @return Last libcm error
39+
**************************************************************************************************/
40+
uint32_t cm_get_lib_error()
41+
{
42+
return cm_error_num;
43+
}

src/cm/process.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cm/err.h>
1010
#include <cm/cm.h>
1111
#include <stdbool.h>
12+
#include <kcmlib.h>
1213

1314
/***************************************************************************************************
1415
* Handling of process events
@@ -25,7 +26,7 @@ static cm_event_handler app_event_handlers[OSIF_PROCESS_EVENTS_COUNT] = { 0 };
2526
bool cm_process_register_event_handler (OSIF_ProcessEvents e, cm_event_handler h)
2627
{
2728
if (app_event_handlers[e] != NULL) {
28-
CM_RETURN_ERROR__ (CM_ERR_EVENT_HANDLER_ALREADY_REGISTERED, false);
29+
CM_RETURN_ERROR (CM_ERR_EVENT_HANDLER_ALREADY_REGISTERED, false);
2930
}
3031

3132
app_event_handlers[e] = h;
@@ -43,7 +44,7 @@ bool cm_process_handle_events()
4344
{
4445
volatile OSIF_ProcessEvent e = { 0 };
4546
if (!cm_process_pop_event ((OSIF_ProcessEvent*)&e)) {
46-
CM_RETURN_ERROR__ (cm_get_os_error(), false);
47+
CM_RETURN_ERROR (cm_get_os_error(), false);
4748
}
4849

4950
switch (e.event) {
@@ -71,7 +72,7 @@ bool cm_process_handle_events()
7172
INT cm_process_create (const char* const filename, bool isKernelMode)
7273
{
7374
if (filename == NULL) {
74-
CM_RETURN_ERROR__ (CM_ERR_INVALID_INPUT, CM_FAILURE);
75+
CM_RETURN_ERROR (CM_ERR_INVALID_INPUT, CM_FAILURE);
7576
}
7677

7778
KProcessFlags flags = PROCESS_FLAGS_NONE;
@@ -85,15 +86,15 @@ INT cm_process_create (const char* const filename, bool isKernelMode)
8586
INT pid = syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)file.startLocation, file.length,
8687
(U32)flags, 0, 0);
8788
if (pid < 0) {
88-
CM_RETURN_ERROR__ (cm_get_os_error(), CM_FAILURE);
89+
CM_RETURN_ERROR (cm_get_os_error(), CM_FAILURE);
8990
}
9091
return pid;
9192
}
9293

9394
INT cm_thread_create (void (*startLocation)(), bool isKernelMode)
9495
{
9596
if (!startLocation) {
96-
CM_RETURN_ERROR__ (CM_ERR_INVALID_INPUT, CM_FAILURE);
97+
CM_RETURN_ERROR (CM_ERR_INVALID_INPUT, CM_FAILURE);
9798
}
9899

99100
KProcessFlags flags = PROCESS_FLAGS_THREAD;
@@ -103,7 +104,7 @@ INT cm_thread_create (void (*startLocation)(), bool isKernelMode)
103104

104105
INT pid = syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)startLocation, 0, (U32)flags, 0, 0);
105106
if (pid < 0) {
106-
CM_RETURN_ERROR__ (cm_get_os_error(), CM_FAILURE);
107+
CM_RETURN_ERROR (cm_get_os_error(), CM_FAILURE);
107108
}
108109
return pid;
109110
}

0 commit comments

Comments
 (0)