forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o Some code cleanup, but mostly implementing a working /dev/rtc, for …
…RTC_RD_TIME anyway.
- Loading branch information
Mike Miller
committed
Nov 24, 2023
1 parent
73395b6
commit 15d6f4e
Showing
11 changed files
with
180 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// RTCDevice.h | ||
// iSH-AOK | ||
// | ||
// Created by Michael Miller on 11/22/23. | ||
// | ||
#include"fs/tty.h" | ||
|
||
extern struct dev_ops rtc_dev; | ||
extern int rtc_close(struct fd *fd); | ||
extern ssize_t rtc_read(struct fd *fd, void *buf, size_t bufsize); | ||
extern int rtc_ioctl(struct fd *fd, int cmd, void *arg); | ||
extern int rtc_poll(struct fd *fd); | ||
extern int rtc_open(int major, int minor, struct fd *fd); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <Foundation/Foundation.h> | ||
#include "fs/poll.h" | ||
#include "fs/dyndev.h" | ||
#include "kernel/errno.h" | ||
#include "debug.h" | ||
#include "fs/devices.h" | ||
|
||
// Real Time Clock file descriptor structure | ||
typedef struct fd rtc_fd; | ||
|
||
// Function to get the current time | ||
typedef struct rtc_time { | ||
int tm_sec; // seconds | ||
int tm_min; // minutes | ||
int tm_hour; // hours | ||
int tm_mday; // day of the month | ||
int tm_mon; // month | ||
int tm_year; // year | ||
} rtc_time; | ||
|
||
static rtc_time *get_current_time(rtc_fd *fd, size_t *len) { | ||
// Obtain the current date | ||
NSDate *currentDate = [NSDate date]; | ||
NSCalendar *calendar = [NSCalendar currentCalendar]; | ||
|
||
// Define the desired date components | ||
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) | ||
fromDate:currentDate]; | ||
|
||
// Allocate and populate the rtc_time structure | ||
rtc_time *timeStruct = malloc(sizeof(rtc_time)); | ||
if (!timeStruct) { | ||
// Handle memory allocation failure | ||
*len = 0; | ||
return NULL; | ||
} | ||
|
||
// Populate the structure | ||
// Note: tm_mon is 0-based (0 for January) and tm_year is years since 1900 | ||
timeStruct->tm_sec = (int)[components second]; | ||
timeStruct->tm_min = (int)[components minute]; | ||
timeStruct->tm_hour = (int)[components hour]; | ||
timeStruct->tm_mday = (int)[components day]; | ||
timeStruct->tm_mon = (int)[components month] - 1; // Adjust for tm_mon | ||
timeStruct->tm_year = (int)[components year] - 1900; // Adjust for tm_year | ||
|
||
// Update the size | ||
*len = sizeof(rtc_time); | ||
|
||
return timeStruct; | ||
} | ||
|
||
|
||
// Read current time into buffer | ||
static ssize_t rtc_read(rtc_fd *fd, void *buf, size_t bufsize) { | ||
@autoreleasepool { | ||
size_t length = 0; | ||
rtc_time *data = get_current_time(fd, &length); | ||
|
||
if (bufsize < length) { | ||
return _EINVAL; // Buffer size is too small | ||
} | ||
|
||
memcpy(buf, data, length); | ||
return length; // Return the number of bytes copied | ||
} | ||
} | ||
|
||
// Polling is not needed for RTC, return 0 | ||
static int rtc_poll(rtc_fd *fd) { | ||
return 0; | ||
} | ||
|
||
// Function to open the RTC device | ||
static int rtc_open(int major, int minor, rtc_fd *fd) { | ||
return 0; | ||
} | ||
|
||
// Function to close the RTC device | ||
static int rtc_close(rtc_fd *fd) { | ||
return 0; | ||
} | ||
|
||
#define RTC_RD_TIME 0x80247009 // Example definition, adjust as necessary | ||
|
||
static ssize_t rtc_ioctl_size(int cmd) { | ||
switch (cmd) { | ||
case RTC_RD_TIME: | ||
return sizeof(rtc_time); | ||
} | ||
return -1; | ||
} | ||
|
||
// Function to handle ioctl operations | ||
static int rtc_ioctl(struct fd *fd, int cmd, void *arg) { | ||
@autoreleasepool { | ||
switch (cmd) { | ||
case RTC_RD_TIME: { // On a real Linux, there are a number of other possible ioctl()'s. We don't really need them | ||
size_t length = 0; | ||
rtc_time *data = get_current_time(fd, &length); | ||
|
||
if (arg == NULL) { | ||
return _EFAULT; // Null pointer argument | ||
} | ||
|
||
*(rtc_time *) arg = *data; // This is the magic that gets the value back to the "kernel" | ||
|
||
return 0; // Success | ||
} | ||
default: | ||
return _EFAULT; // Oops | ||
} | ||
} | ||
} | ||
|
||
struct dev_ops rtc_dev = { | ||
.open = rtc_open, | ||
.fd.read = rtc_read, | ||
.fd.poll = rtc_poll, | ||
.fd.close = rtc_close, | ||
.fd.ioctl = rtc_ioctl, | ||
.fd.ioctl_size = rtc_ioctl_size, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.