Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dryos.h>
- #include <module.h>
- #include <bmp.h>
- #include <menu.h>
- #define MODULES_DIR "B:/ML/MODULES"
- /* download url to the file specified */
- int32_t net_get_file(char *url, char *dst_file);
- int32_t net_get_data(char *url, void (*cbr)(void *ctx, char *data, uint32_t length));
- /* this is not perfect, as .Mo and .mO aren't detected. important? */
- static int module_valid_filename(char* filename)
- {
- int n = strlen(filename);
- if ((n > 3) && (streq(filename + n - 3, ".MO") || streq(filename + n - 3, ".mo")) && (filename[0] != '.') && (filename[0] != '_'))
- return 1;
- return 0;
- }
- static const char * format_date(unsigned timestamp )
- {
- static char datestr[17];
- int year=1970; // Unix Epoc begins 1970-01-01
- int month=11; // This will be the returned MONTH NUMBER.
- int day; // This will be the returned day number.
- int hour;
- int minute;
- int second;
- int dayInSeconds=86400; // 60secs*60mins*24hours
- int daysInYear=365; // Non Leap Year
- int daysInLYear=daysInYear+1; // Leap year
- int days=timestamp/dayInSeconds; // Days passed since UNIX Epoc
- int sec_of_day=timestamp % dayInSeconds;
- int tmpDays=days+1; // If passed (timestamp < dayInSeconds), it will return 0, so add 1
- while(tmpDays>=daysInYear) // Start adding years to 1970
- {
- year++;
- if ((year)%4==0&&((year)%100!=0||(year)%400==0)) tmpDays-=daysInLYear; else tmpDays-=daysInYear;
- }
- int monthsInDays[12] = {-1,30,59,90,120,151,181,212,243,273,304,334};
- if (!(year)%4==0&&((year)%100!=0||(year)%400==0)) // The year is not a leap year
- {
- monthsInDays[0] = 0;
- monthsInDays[1] =31;
- }
- while (month>0)
- {
- if (tmpDays>monthsInDays[month]) break; // month+1 is now the month number.
- month--;
- }
- day=tmpDays-monthsInDays[month]; // Setup the date
- month++; // Increment by one to give the accurate month
- if (day==0) {year--; month=12; day=31;} // Ugly hack but it works, eg. 1971.01.00 -> 1970.12.31
- hour = (sec_of_day / (60 * 60)) % 24;
- minute = (sec_of_day / 60) % 60;
- second = sec_of_day % 60;
- snprintf(datestr, sizeof(datestr), "%04d%02d%02d_%02d%02d%02d", year, month, day, hour, minute, second);
- return datestr;
- }
- uint32_t date_cbr_done = 0;
- char *date_cbr_data = 0;
- uint32_t date_cbr_length = 0;
- void modmgr_date_cbr(void *ctx, char *buf, uint32_t length)
- {
- date_cbr_data = buf;
- date_cbr_length = length;
- date_cbr_done = 1;
- }
- static void modmgr_task()
- {
- console_show();
- beep();
- msleep(1000);
- printf("Scanning modules...\n");
- struct fio_file file;
- struct fio_dirent * dirent = FIO_FindFirstEx(MODULES_DIR, &file );
- if( IS_ERROR(dirent) )
- {
- NotifyBox(2000, "Module dir missing" );
- return;
- }
- do
- {
- if (file.mode & ATTR_DIRECTORY) continue; // is a directory
- if (module_valid_filename(file.name))
- {
- printf(" File '%s'\n", file.name);
- printf(" Date '%s'\n", format_date(file.timestamp));
- /* make a lower case filename */
- char lower[64];
- strcpy(lower, file.name);
- for(int pos = 0; pos < strlen(lower); pos++)
- {
- if(lower[pos] >= 'A' && lower[pos] <= 'Z')
- {
- lower[pos] += 0x20;
- }
- }
- char url[64];
- snprintf(url, sizeof(url), "http://ml.g3gg0.de/modules/%s/latest_date", lower);
- printf(" Fetch '%s'\n", url);
- date_cbr_done = 0;
- net_get_data(url, modmgr_date_cbr);
- while(!date_cbr_done)
- {
- msleep(50);
- }
- if(date_cbr_length)
- {
- printf(" Curr '%s'\n", date_cbr_data);
- if(!strcmp(date_cbr_data, format_date(file.timestamp)))
- {
- printf(" --- is latest ---\n");
- }
- else
- {
- printf(" === NEED UPDATE ===\n");
- char dl_url[64];
- char dl_filename[64];
- snprintf(dl_url, sizeof(dl_url), "http://ml.g3gg0.de/modules/%s/%s", lower, lower);
- snprintf(dl_filename, sizeof(dl_filename), MODULES_DIR"/%s", file.name);
- printf(" Fetch '%s'\n", dl_url);
- net_get_file(dl_url, dl_filename);
- printf(" ===== UPDATED =====\n");
- }
- }
- else
- {
- printf(" Curr FAILED (no such module?)\n");
- }
- }
- } while( FIO_FindNextEx( dirent, &file ) == 0);
- FIO_FindClose(dirent);
- }
- static struct menu_entry modmgr_menu[] =
- {
- {
- .name = "Module Manager",
- .help = "",
- .submenu_width = 710,
- .children = (struct menu_entry[])
- {
- {
- .name = "Check modules",
- .priv = modmgr_task,
- .select = run_in_separate_task,
- },
- MENU_EOL,
- },
- },
- };
- static unsigned int modmgr_init()
- {
- /* still need to find a suitable menu */
- menu_add("Audio", modmgr_menu, COUNT(modmgr_menu));
- return 0;
- }
- static unsigned int modmgr_deinit()
- {
- return 0;
- }
- MODULE_INFO_START()
- MODULE_INIT(modmgr_init)
- MODULE_DEINIT(modmgr_deinit)
- MODULE_INFO_END()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement