]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
attach/detach working (minus options)
authorLeo C <erbl259-lmu@yahoo.de>
Mon, 30 May 2016 21:58:57 +0000 (23:58 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Mon, 30 May 2016 21:58:57 +0000 (23:58 +0200)
avr/cmd_attach.c
avr/z180-serv.c
include/z180-serv.h

index e1a3b2c9052467403a6c0990c36eb146c357f461..9fbfa1e68fbdeda9d98643cb20909df98c384c45 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 /*
- * attach chanels to devices
+ * attach channels to devices
  */
 
 #include "common.h"
 #include "command.h"
 #include "z180-serv.h"
 #include "getopt-min.h"
-//#include "print-utils.h"
-//#include "con-utils.h"
-//#include "timer.h"
-//#include "z80-if.h"
-//#include "debug.h"
 
 
 static const FLASH char * const FLASH rc_messages[] = {
@@ -68,17 +63,17 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
        uint8_t unit;
        char *filename = NULL;
        bool detach = false;
+       bool detach_all = false;
        drv_opt_t options = 0;
        int res;
 
        (void) cmdtp; (void) flag;
 
 
-       if (argv[0][0] == 'd')
+       if (argv[0][0] == 'd') {
                /* we are called as 'detach' */
                detach = true;
-
-       if (argc == 1) {
+       } else if (argc == 1) {
                /* no arguments */
                drv_list();
                return CMD_RET_SUCCESS;
@@ -88,11 +83,14 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
        optind = 1;
 
        int opt;
-       while ((opt = getopt(argc, argv, PSTR("drwo:"))) != -1) {
+       while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) {
                switch (opt) {
                case 'd':
                        detach = true;
                        break;
+               case 'a':
+                       detach_all = true;
+                       break;
                case 'r':
                        options |= DRV_OPT_RO;
                        break;
@@ -128,21 +126,26 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        /* remaining arguments */
        argc -= optind;
-       if ( !((detach && argc == 1) ||
-                       ((options & DRV_OPT_REATTATCH) && argc == 1) ||
-                       argc == 2) )
+       if ( !( (argc == 0 && detach && detach_all) ||
+                       (argc == 1 && detach) ||
+                       (argc == 1 && (options & DRV_OPT_REATTATCH)) ||
+                        argc == 2) )
                return CMD_RET_USAGE;
 
-       if ((strlen(argv[optind]) != 4) ||
+       if (argc > 0 && ((strlen(argv[optind]) != 4) ||
                        strncmp_P(argv[optind], PSTR("dsk"), 3) ||
-                       (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE) {
+                       (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE)) {
 
                printf_P(PSTR("Unknown device: '%s'\n"), argv[optind]);
                return CMD_RET_FAILURE;
        }
 
        if (detach) {
-               res = drv_detach(unit);
+               if (detach_all)
+                       for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++)
+                               drv_detach(i);
+               else
+                       drv_detach(unit);
                return CMD_RET_SUCCESS;
        }
 
index 1bf76b4d115df95d2b22e218501db99531ef7090..ddb4622be4707bc3edb1a80c9bf5720c8bece717 100644 (file)
@@ -235,9 +235,9 @@ int drv_detach(uint8_t drv)
 
                if (p->img_name) {
                        f_close(&p->fd);
-                       p->opt = 0;
-                       p->dirty = false;
                        free(p->img_name);
+                       p->opt = 0;
+                       p->flags &= ~DRV_FLG_DIRTY;
                        p->img_name = NULL;
                }
        }
@@ -284,7 +284,7 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
                if (drv_find_file_attached(filename) >= 0)
                        return AT_OTHER;
 
-               p->opt = options & ~DRV_OPT_REATTATCH;
+               p->opt = options;
 
                /* new attachment */
 
@@ -307,7 +307,7 @@ int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
                                if (res || bw < CONFIG_CPM_BLOCK_SIZE) {
                                        debug_cpmsd("               failed! res: %d, bytes written: %u\n",      res, bw);
                                }
-                               p->dirty = true;
+                               p->flags |= DRV_FLG_DIRTY;
                                bg_setstat(handle_cpm_drv_to, 1);
                        }
                }
@@ -337,9 +337,9 @@ int cpm_drv_to(int state)
        case 2:
                if (get_timer(ts) > 1000) {
                        for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) {
-                               if (drv_table[i].dirty) {
+                               if (drv_table[i].flags & DRV_FLG_DIRTY) {
                                        f_sync(&drv_table[i].fd);
-                                       drv_table[i].dirty = false;
+                                       drv_table[i].flags &= ~DRV_FLG_DIRTY;
                                        debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i + CONFIG_CPM_BASE_DRIVE);
                                }
                        }
@@ -378,9 +378,9 @@ void msg_cpm_result(uint8_t subf, uint8_t rc, int res)
 
 void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
 {
-       FRESULT res = 0;
        uint8_t drv;
-       char *np;
+       struct cpm_drive_s *p;
+       FRESULT res = 0;
 
        (void)subf;
 
@@ -393,52 +393,22 @@ void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
        /* Get relative drive number */
        drv = msg[1];
        if ( drv >= CONFIG_CPM_MAX_DRIVE) {
+               debug_cpmsd("##           failed: invalid relative drive number '%d'\n", drv);
                return msg_cpm_result(subf, 0x02, res);
        }
 
+       p = &drv_table[drv];
+       p->flags &= ~DRV_FLG_OPEN;
+       p->dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
 
-       drv_table[drv].dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
-
-
-       if (drv_table[drv].img_name != NULL) {
-               debug_cpmsd("## %7lu  close: '%s'\n", get_timer(0), drv_table[drv].img_name);
-               f_close(&drv_table[drv].fd);
-               drv_table[drv].dirty = false;
-               free(drv_table[drv].img_name);
-               drv_table[drv].img_name = NULL;
-       }
 
-       strcpy_P((char *)disk_buffer, PSTR("dsk0"));
-       disk_buffer[3] = msg[0] + '0';
-       if (((np = getenv_char((char*)disk_buffer)) == NULL) ||
-                       ((drv_table[drv].img_name = strdup(np)) == NULL)) {
+       if (p->img_name == NULL) {
+               /* no file attached */
+               debug_cpmsd("##           failed: no file attached:\n");
                return msg_cpm_result(subf, 0x03, res);
        }
 
-
-       res = f_open(&drv_table[drv].fd, drv_table[drv].img_name,
-                       FA_WRITE | FA_READ);
-
-    debug_cpmsd("## %7lu   open: '%s', (env: '%s'), res: %d\n", get_timer(0),
-                       drv_table[drv].img_name, disk_buffer, res);
-
-       if (!res && f_size(&drv_table[drv].fd) < CONFIG_CPM_DISKSIZE) {
-               unsigned int bw;
-
-               debug_cpmsd("            expanding image file from %ld to %ld\n",
-                               f_size(&drv_table[drv].fd), CONFIG_CPM_DISKSIZE);
-
-               res = f_lseek(&drv_table[drv].fd, CONFIG_CPM_DISKSIZE-CONFIG_CPM_BLOCK_SIZE);
-               if (!res) {
-                       memset(disk_buffer, 0xe5, CONFIG_CPM_BLOCK_SIZE);
-                       res = f_write(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &bw);
-                       if (res || bw < CONFIG_CPM_BLOCK_SIZE) {
-                               debug_cpmsd("               failed! res: %d, bytes written: %u\n",      res, bw);
-                       }
-                       drv_table[drv].dirty = true;
-                       bg_setstat(handle_cpm_drv_to, 1);
-               }
-       }
+       p->flags |= DRV_FLG_OPEN;
 
        /* send  result*/
        msg_cpm_result(subf, 0x00, res);
@@ -535,7 +505,7 @@ void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
        }
 
        if (dowrite && !res) {
-               drv_table[drv].dirty = true;
+               drv_table[drv].flags |= DRV_FLG_DIRTY;
                bg_setstat(handle_cpm_drv_to, 1);
        }
 
index 5385ecd9df21b43c1e79efc36fc9b3a440e4d73f..b6a65b0b80aa51404bf7730343d5d3b865594970 100644 (file)
@@ -25,9 +25,13 @@ typedef uint8_t drv_opt_t;
 #define DRV_OPT_DEBUG          (1<<1)  /* Debug this drive */
 #define DRV_OPT_REATTATCH      (1<<7)  /* Change existing attachment */
 
+typedef uint8_t drv_flag_t;
+#define DRV_FLG_OPEN           (1<<0)  /* Drive is logged in from CP/M */
+#define DRV_FLG_DIRTY          (2<<0)  /* Unwritten data */
+
 struct cpm_drive_s {
        drv_opt_t       opt;
-       bool            dirty;
+       drv_flag_t      flags;
        uint32_t        dph;
        char            *img_name;
        FIL                     fd;