Skip to content

Commit 72accec

Browse files
author
Mike Miller
committed
o Add working emulated RTC (Real Time Clock), some cleanup
1 parent 2f276c8 commit 72accec

File tree

9 files changed

+154
-20
lines changed

9 files changed

+154
-20
lines changed

app/AppDelegate.m

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "fs/dyndev.h"
2828
#include "fs/devices.h"
2929
#include "fs/path.h"
30+
#include "app/RTCDevice.h"
3031

3132
#if ISH_LINUX
3233
#import "LinuxInterop.h"
@@ -173,7 +174,11 @@ - (intptr_t)boot {
173174
if (err != 0)
174175
return err;
175176
generic_mknodat(AT_PWD, "/dev/location", S_IFCHR|0666, dev_make(DYN_DEV_MAJOR, DEV_LOCATION_MINOR));
176-
// The following does nothing for now. Placeholder
177+
178+
// Emulate a RTC, read time only
179+
err = dyn_dev_register(&rtc_dev, DEV_CHAR, DEV_RTC_MAJOR, DEV_RTC_MINOR);
180+
if (err != 0)
181+
return err;
177182
generic_mknodat(AT_PWD, "/dev/rtc0", S_IFCHR|0666, dev_make(DEV_RTC_MAJOR, DEV_RTC_MINOR));
178183
generic_symlinkat("/dev/rtc0", AT_PWD, "/dev/rtc");
179184

app/RTCDevice.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// RTCDevice.h
3+
// iSH-AOK
4+
//
5+
// Created by Michael Miller on 11/24/23.
6+
//
7+
8+
extern struct dev_ops rtc_dev;

app/RTCDevice.m

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//
2+
// RTCDevice.m
3+
// iSH-AOK
4+
//
5+
// Created by Michael Miller on 11/24/23.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#include "fs/poll.h"
10+
#include "fs/dyndev.h"
11+
#include "kernel/errno.h"
12+
#include "debug.h"
13+
#include "fs/devices.h"
14+
15+
// Real Time Clock file descriptor structure
16+
typedef struct fd rtc_fd;
17+
18+
// Need to define this, as we are running on iOS, which doesn't have/give access to the RTC
19+
typedef struct rtc_time {
20+
int tm_sec; // seconds
21+
int tm_min; // minutes
22+
int tm_hour; // hours
23+
int tm_mday; // day of the month
24+
int tm_mon; // month
25+
int tm_year; // year
26+
} rtc_time;
27+
28+
// Get the time, put it in the appropriate structure
29+
static rtc_time *get_current_time(rtc_fd *fd, size_t *len) {
30+
// Obtain the current date
31+
NSDate *currentDate = [NSDate date];
32+
NSCalendar *calendar = [NSCalendar currentCalendar];
33+
34+
// Define the desired date components
35+
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
36+
fromDate:currentDate];
37+
38+
// Allocate and populate the rtc_time structure
39+
rtc_time *timeStruct = malloc(sizeof(rtc_time));
40+
if (!timeStruct) {
41+
// Handle memory allocation failure
42+
*len = 0;
43+
return NULL;
44+
}
45+
46+
// Populate the structure
47+
// Note: tm_mon is 0-based (0 for January) and tm_year is years since 1900
48+
timeStruct->tm_sec = (int)[components second];
49+
timeStruct->tm_min = (int)[components minute];
50+
timeStruct->tm_hour = (int)[components hour];
51+
timeStruct->tm_mday = (int)[components day];
52+
timeStruct->tm_mon = (int)[components month] - 1; // Adjust for tm_mon
53+
timeStruct->tm_year = (int)[components year] - 1900; // Adjust for tm_year
54+
55+
// Update the size
56+
*len = sizeof(rtc_time);
57+
58+
return timeStruct;
59+
}
60+
61+
static ssize_t rtc_read(rtc_fd *fd, void *buf, size_t bufsize) {
62+
return 0;
63+
}
64+
65+
static int rtc_poll(rtc_fd *fd) {
66+
return 0;
67+
}
68+
69+
static int rtc_open(int major, int minor, rtc_fd *fd) {
70+
return 0;
71+
}
72+
73+
static int rtc_close(rtc_fd *fd) {
74+
return 0;
75+
}
76+
77+
#define RTC_RD_TIME 0x80247009 // Example definition, adjust as necessary
78+
79+
static ssize_t rtc_ioctl_size(int cmd) {
80+
switch (cmd) {
81+
case RTC_RD_TIME:
82+
return sizeof(rtc_time);
83+
}
84+
return -1;
85+
}
86+
87+
// Function to handle ioctl operations
88+
static int rtc_ioctl(struct fd *fd, int cmd, void *arg) {
89+
@autoreleasepool {
90+
switch (cmd) {
91+
case RTC_RD_TIME: { // On a real Linux, there are a number of other possible ioctl()'s. We don't really need them
92+
size_t length = 0;
93+
rtc_time *data = get_current_time(fd, &length);
94+
95+
if (arg == NULL) {
96+
return _EFAULT; // Null pointer argument
97+
}
98+
99+
*(rtc_time *) arg = *data; // This is the magic that gets the value back to the "kernel"
100+
101+
return 0; // Success
102+
}
103+
default:
104+
return _EFAULT; // Oops
105+
}
106+
}
107+
}
108+
109+
struct dev_ops rtc_dev = {
110+
.open = rtc_open,
111+
.fd.read = rtc_read,
112+
.fd.poll = rtc_poll,
113+
.fd.close = rtc_close,
114+
.fd.ioctl = rtc_ioctl,
115+
.fd.ioctl_size = rtc_ioctl_size, // Do NOT FORGET THIS if you want to implement ioctl(s) for your device. They will not work without it.
116+
};

fs/dev.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "fs/tty.h"
66
#include "fs/dyndev.h"
77
#include "fs/devices.h"
8+
#include "app/RTCDevice.h"
89

910
struct dev_ops *block_devs[256] = {
1011
// no block devices yet
@@ -15,7 +16,7 @@ struct dev_ops *char_devs[256] = {
1516
[TTY_ALTERNATE_MAJOR] = &tty_dev,
1617
[TTY_PSEUDO_MASTER_MAJOR] = &tty_dev,
1718
[TTY_PSEUDO_SLAVE_MAJOR] = &tty_dev,
18-
// [DEV_RTC_MAJOR] = &rtc_dev_char,
19+
[DEV_RTC_MAJOR] = &rtc_dev,
1920
[DYN_DEV_MAJOR] = &dyn_dev_char,
2021
};
2122

fs/dev.h

-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ static inline dev_t_ dev_fake_from_real(dev_t dev) {
3333
#define DEV_BLOCK 0
3434
#define DEV_CHAR 1
3535

36-
struct dev_rtc {
37-
intptr_t (*open)(int major, int minor, struct fd *fd);
38-
int (*close)(int major, int minor, struct fd *fd);
39-
ssize_t (*read)(void *buf, size_t count);
40-
};
41-
4236
struct dev_ops {
4337
intptr_t (*open)(int major, int minor, struct fd *fd);
4438
struct fd_ops fd;

fs/devices.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#define DYN_DEV_MAJOR 240
3838
// /dev/rtc
3939
#define DEV_RTC_MAJOR 252
40-
#define DEV_RTC_MINOR 0
40+
#define DEV_RTC_MINOR 2
4141

4242

4343
// /dev/clipboard

fs/dyndev.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int dyn_dev_register(struct dev_ops *ops, int type, int major, int minor) {
2626
if (minor < 0 || minor > MAX_MINOR) {
2727
return _EINVAL;
2828
}
29-
if (major != DYN_DEV_MAJOR) {
29+
if ((major != DYN_DEV_MAJOR) && (major != DEV_RTC_MAJOR)) {
3030
return _EINVAL;
3131
}
3232
if (ops == NULL) {

iSH-AOK.xcodeproj/project.pbxproj

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
5D8ACEFA284BF122003C50D3 /* net.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEF9284BF122003C50D3 /* net.c */; };
108108
5D8ACEFD284CE096003C50D3 /* sys.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEFC284CE096003C50D3 /* sys.c */; };
109109
5D8ACEFE284CE096003C50D3 /* sys.c in Sources */ = {isa = PBXBuildFile; fileRef = 5D8ACEFC284CE096003C50D3 /* sys.c */; };
110+
5D8CFA852B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
111+
5D8CFA862B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
112+
5D8CFA872B1198B300D50E57 /* RTCDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D8CFA842B1198B300D50E57 /* RTCDevice.m */; };
110113
5D9897D028B6B953003D3670 /* AppStore.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 5D9897CE28B6B953003D3670 /* AppStore.xcconfig */; };
111114
5DA0A8342AAE21D000397280 /* BatteryStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA0A8332AAE21D000397280 /* BatteryStatus.m */; };
112115
5DD383EB2AAE33330013A847 /* UIDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DD383EA2AAE33330013A847 /* UIDevice.m */; };
@@ -589,6 +592,8 @@
589592
5D8ACEFC284CE096003C50D3 /* sys.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sys.c; sourceTree = "<group>"; };
590593
5D8C94D3287E53AD0095EDEE /* fp_sync.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = fp_sync.c; sourceTree = "<group>"; };
591594
5D8CEFF92894C81400D24ED8 /* resource_locking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = resource_locking.h; sourceTree = "<group>"; };
595+
5D8CFA842B1198B300D50E57 /* RTCDevice.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RTCDevice.m; sourceTree = "<group>"; };
596+
5D8CFA882B1198DB00D50E57 /* RTCDevice.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RTCDevice.h; sourceTree = "<group>"; };
592597
5D9897CE28B6B953003D3670 /* AppStore.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = AppStore.xcconfig; sourceTree = "<group>"; };
593598
5D9897CF28B6B953003D3670 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
594599
5DA0A8332AAE21D000397280 /* BatteryStatus.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BatteryStatus.m; sourceTree = "<group>"; };
@@ -1090,6 +1095,8 @@
10901095
BB88F4A4215476BA00A341FD /* iSH.entitlements */,
10911096
BB88F4912154760800A341FD /* FileProvider */,
10921097
BB41591D255EF9E300E0950C /* UITests */,
1098+
5D8CFA842B1198B300D50E57 /* RTCDevice.m */,
1099+
5D8CFA882B1198DB00D50E57 /* RTCDevice.h */,
10931100
);
10941101
path = app;
10951102
sourceTree = "<group>";
@@ -2122,6 +2129,7 @@
21222129
buildActionMask = 2147483647;
21232130
files = (
21242131
5DA0A8342AAE21D000397280 /* BatteryStatus.m in Sources */,
2132+
5D8CFA852B1198B300D50E57 /* RTCDevice.m in Sources */,
21252133
5DD383EB2AAE33330013A847 /* UIDevice.m in Sources */,
21262134
BB28C7BB268975AE00BDC834 /* LocationDevice.m in Sources */,
21272135
5D8ACEFD284CE096003C50D3 /* sys.c in Sources */,
@@ -2147,6 +2155,7 @@
21472155
BB88F49A2154760800A341FD /* FileProviderEnumerator.m in Sources */,
21482156
BBBFE94921C5CFF100509DD5 /* NSError+ISHErrno.m in Sources */,
21492157
BB9C7B87240A2B1E00F5D4F0 /* AppGroup.m in Sources */,
2158+
5D8CFA872B1198B300D50E57 /* RTCDevice.m in Sources */,
21502159
BB88F4942154760800A341FD /* FileProviderExtension.m in Sources */,
21512160
BB88F4972154760800A341FD /* FileProviderItem.m in Sources */,
21522161
);
@@ -2172,6 +2181,7 @@
21722181
BB28C79226896B1F00BDC834 /* AboutAppearanceViewController.m in Sources */,
21732182
BB28C79326896B1F00BDC834 /* AltIconViewController.m in Sources */,
21742183
491B31E62883BF22008EEFB0 /* ThemeViewController.m in Sources */,
2184+
5D8CFA862B1198B300D50E57 /* RTCDevice.m in Sources */,
21752185
BB28C7B226896C4600BDC834 /* main.m in Sources */,
21762186
496C7AB227CC697E005D7613 /* Theme.m in Sources */,
21772187
BB28C79426896B1F00BDC834 /* AboutNavigationController.m in Sources */,

util/sync.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ static inline void complex_lockt(lock_t *lock, int log_lock, __attribute__((unus
162162
}
163163

164164
lock->owner = pthread_self();
165-
lock->pid = current_pid();
166-
lock->uid = current_uid();
167-
strncpy(lock->comm, current_comm(), sizeof(lock->comm) - 1);
168-
lock->comm[sizeof(lock->comm) - 1] = '\0'; // Null-terminate just in case
165+
//lock->pid = current_pid();
166+
//lock->uid = current_uid();
167+
//strncpy(lock->comm, current_comm(), sizeof(lock->comm) - 1);
168+
//lock->comm[sizeof(lock->comm) - 1] = '\0'; // Null-terminate just in case
169169
}
170170

171171
static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) const char *file, __attribute__((unused)) int line) {
@@ -174,16 +174,16 @@ static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) co
174174
pthread_mutex_lock(&lock->m);
175175
modify_locks_held_count_wrapper(1);
176176
lock->owner = pthread_self();
177-
lock->pid = current_pid();
178-
lock->uid = current_uid();
179-
strncpy(lock->comm, current_comm(), 16);
177+
//lock->pid = current_pid();
178+
//lock->uid = current_uid();
179+
//strncpy(lock->comm, current_comm(), 16);
180180
modify_critical_region_counter_wrapper(-1, __FILE__, __LINE__);
181181
} else {
182182
pthread_mutex_lock(&lock->m);
183183
lock->owner = pthread_self();
184-
lock->pid = current_pid();
185-
lock->uid = current_uid();
186-
strncpy(lock->comm, current_comm(), 16);
184+
//lock->pid = current_pid();
185+
//lock->uid = current_uid();
186+
//strncpy(lock->comm, current_comm(), 16);
187187
}
188188
return;
189189
}

0 commit comments

Comments
 (0)